| Author |
reading and writing text file using java - detect newline
|
Arjun Singh Rampal
Greenhorn
Joined: May 24, 2012
Posts: 20
|
|
|
I am reading a text file (word by word) with java code, converting it all to lower case and then writing it to another file. Can you tell me how to jump to a new line when my code sees a newline in source file ? I dont want all the stuff to come on one line.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4753
|
|
Arjun Singh Rampal wrote:I am reading a text file (word by word) with java code, converting it all to lower case and then writing it to another file. Can you tell me how to jump to a new line when my code sees a newline in source file ? I dont want all the stuff to come on one line.
Generally, the easiest way is with BufferedReader and either PrintWriter or BufferedWriter; but the best advice is probably to read the tutorials.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Arjun Singh Rampal
Greenhorn
Joined: May 24, 2012
Posts: 20
|
|
Winston Gutkowski wrote:
Arjun Singh Rampal wrote:I am reading a text file (word by word) with java code, converting it all to lower case and then writing it to another file. Can you tell me how to jump to a new line when my code sees a newline in source file ? I dont want all the stuff to come on one line.
Generally, the easiest way is with BufferedReader and either PrintWriter or BufferedWriter; but the best advice is probably to read the tutorials.
Winston
Yes, the tutorials are fine. But right now, i want to see how i can jump to a new line in my output file when i encounter a new line in my input file. Can you give me the code snippet ?
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
Arjun Singh Rampal wrote:
how i can jump to a new line in my output file when i encounter a new line in my input file.
If you want to use the platform-defined way of "jumping to a new line", use the method
println
Otherwise include the appropriate characters at every end of line.
|
 |
Dustin Weber
Greenhorn
Joined: May 25, 2012
Posts: 3
|
|
If you use bufferedwriter there is a method called newLine(); that will make an empty line in your text document.
Also in your write(); method you can put the "\n" which will do the exact same thing.
What I normally do is look through the API to find methods. There are a lot of neat ones out there! Like String split. Ingenious!
Hope that helps,
- Dustin
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
Dustin Weber wrote: . . . you can put the "\n" which will do the exact same thing. . . .
Everybody thinks it does the same thing, and you find it in all the books, but it does something different. It adds the line‑feed character 0x000a. If you use newLine() on Windows® it adds two characters, CR‑LF or 0x000d‑0x000a
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5886
|
|
Arjun Singh Rampal wrote:
Yes, the tutorials are fine.
Yes, they are, and you should use them.
But right now, i want to see how i can jump to a new line in my output file when i encounter a new line in my input file.
Then you should work through the tutorials, study the javadocs for the relevant classes, and see if you can figure out how to make it work. If you get stuck, post what you have and what particular difficulties you're encountering.
Can you give me the code snippet ?
This site is NotACodeMill.(⇐click) If you want examples of something similar, there are plenty to be found by searching the web.
Good luck!
|
 |
 |
|
|
subject: reading and writing text file using java - detect newline
|
|
|