How to identify / process new lines of text in Java?

I get files in different formats coming from different systems that I need to import into our database. Part of the import process is to check the length of the string to make sure the format is correct. We seem to have problems with files coming from UNIX systems where one character is added. I suspect this is because the return carriage is encoded differently on the UNIX and Windows platforms.

Is there any way to determine on which file system the file was created, besides checking the last character in the string? Or maybe a way to read files as text, rather than binary, which I suspect is a problem?

Thanks guys!

+3
source share
3 answers

Unix systems use line endings \n, while on Windows it uses \r\n, and mac uses \r. You cannot detect the file system, since it does not matter. I can use \ n on Windows if my editor supports it, for example. This is just the standard for these OSs, not a requirement.

- , , , - \n a\r, , \r, \n, . , . :

  • \n,
  • a\r, , char -\n, , .
+5

Java , \n (unix) \r\n (windows) \r (mac), ( ). . java.io.FileReader . Unicode.

, . . java.io.DataInputStream .

+1

, , ?

. - . DOS UNIX .

, , , , , , ?

Yes. Open the file with a file reader, wrap it in a buffered reader, and use the method readLine()to read the file in a line temporarily. This method recognizes "\n", "\r"or "\r\n"as a line separator, and therefore works for DOS, UNIX, and Mac files.

Here is a typical code:

    Reader r = new FileReader("somefile");
    try {
        BufferedReader br = new BufferedReader(r);
        String line;
        while ((line = r.readLine()) != null) {
            // process line
        }
    } finally {
        r.close();
    }
0
source

Source: https://habr.com/ru/post/1749570/


All Articles