Atish Panghate wrote:when I open the file in Unix all values are in a single line.
Atish Panghate wrote:I have used System.getProperty("line.separator") as the way to get new line for platform independence. But the result are same.
Atish Panghate wrote:I am reading XML fiel on Linux and have used \n as line separator. Still it doest work. getBytes method somehow not able to write encoding for \n I guess. Please suggest.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Read files, line by line, with a BufferedReader or a Scanner...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
…and you read it like this:-so I tried it, and got this output:-Campbell Ritchie is a right nuisance.↩
The read method reads the character and returns it as an integer. (Remember a char is an integer, not a letter.) It is rather like System.in.read, and I don't like it. You see the ASCII values of the first two letters appear, followed not by the line but by the remainder of the line; I think you would get an empty String as the result if you had a two‑character line there. Try it and see what happens.java ReaderDemo file
67
97
mpbell Ritchie is a right nuisance.
That is quite clear, but it isn't what the methods is called and lots of people misunderstand it. Even books like Horstmann misunderstand it.Assume you are using the default delimiter for inScan, and you feed this input:-This method returns the rest of the current line, excluding any line separator at the end.
… then you will have "Campbell Ritchie" as name. You do not get the line, but the rest of the line, after 789 .123 456 789 Campbell Ritchie↩
It reads the remainder of the line, which now equals "" into name: a zero‑length String. "Campbell Ritchie" remains unused, and will cause Exceptions if you try to read it with nextInt() or similar.123 456 789↩
Campbell Ritchie↩
Atish Panghate wrote:I am reading XML fiel on Linux and have used \n as line separator. Still it doest work. getBytes method somehow not able to write encoding for \n I guess. Please suggest.
And Prasanna Raman found the daft mistake. The loop would read alternate lines. Maybe a do loop would be betterCampbell Ritchie wrote: . . . more difficult. . . . please check whether I have made some daft mistake. . . .
Campbell Ritchie wrote:Suggestion: search my posts for utility class, and work out how to write a utility class. Search them for Scanner next Int hasNextInt. You will find a loop which can absolutely obviate the risk of Exceptions from nextInt() or similar, by printing out an error message and asking for new input. Rob Spoor showed me how to do that.
I usually search both BJ and JiG if I am looking for that sort of post.Chan Ag wrote: . . . I should have searched in Beginning Java. I was searching in JIG. . . .