Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

newline - How to write new line character to a file in Java

I have a string that contains new lines. I send this string to a function to write the String to a text file as:

    public static void writeResult(String writeFileName, String text)
    {
        try
        {
        FileWriter fileWriter = new FileWriter(writeFileName);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write(text);

        // Always close files.
        bufferedWriter.close();

        }
        catch(IOException ex) {
            System.out.println("Error writing to file '"+ writeFileName + "'");}
    } //end writeResult function

But when I open the file, I find it without any new lines. When I display the text in the console screen, it is displayed with new lines. How can I write the new line character in the text file.

EDIT: Assume this is the argument text that I sent to the function above:

I returned from the city about three o'clock on that
may afternoon pretty well disgusted with life.
I had been three months in the old country, and was

How to write this string as it is (with new lines) in the text file. My function write the string in one line. Can you provide me with a way to write the text to the file including new lines ?

EDIT 2: The text is originally in a .txt file. I read the text using:

while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('
'); //append new line
} //end while

where sb is a StringBuffer

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In EDIT 2:

while((line = bufferedReader.readLine()) != null)
{
  sb.append(line); //append the lines to the string
  sb.append('
'); //append new line
} //end while

you are reading the text file, and appending a newline to it. Don't append newline, which will not show a newline in some simple-minded Windows editors like Notepad. Instead append the OS-specific line separator string using:

sb.append(System.lineSeparator()); (for Java 1.7 and 1.8) or sb.append(System.getProperty("line.separator")); (Java 1.6 and below)

Alternatively, later you can use String.replaceAll() to replace " " in the string built in the StringBuffer with the OS-specific newline character:

String updatedText = text.replaceAll(" ", System.lineSeparator())

but it would be more efficient to append it while you are building the string, than append ' ' and replace it later.

Finally, as a developer, if you are using notepad for viewing or editing files, you should drop it, as there are far more capable tools like Notepad++, or your favorite Java IDE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...