I’m triying to read the text file below with a java.util.Scanner in a simple Java Program.
0001;GUAJARA-MIRIM;RO
0002;ALTO ALEGRE DOS PARECIS;RO
0003;PORTO VELHO;RO
I read the text file using the code below:
scanner = new Scanner(filerader).useDelimiter("\\;|\\n");
while (scanner.hasNext()) {
int id= scanner.nextInt();
String name = scanner.next();
String code = scanner.next();
System.out.printf(".%s.%s.%d.\n", name, code, id);
}
The results are:
.GUAJARA-MIRIM.RO.1
.
.ALTO ALEGRE DOS PARECIS.RO.2
.
.PORTO VELHO.RO.3
.
But the result of the third token of each line has an incovenient ‘\r’ caracther at the end (ANSI code 13). I have no idea why (I used the ‘.’ character on the formatting string to to make it clear where the ‘\r’ is).
So,
- Why there’s a ‘\r’ at the end of the third token?
- How to bypass it.
It is very simple to use an workaround like code.substring(0, 2), but instead I want to understand why there’s a ‘\r’ character there.
In some file systems(specially Windows),
\r\nis used a new line character. You are using\nonly a delimiter so\rremain out. Add\ralso in your delimiters.To make your code little more robust, use
System.lineSeparator()to get the new line characters and use the delimiters accordingly.