java 行尾,FileWriter \n

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10280865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:21:49  来源:igfitidea点击:

End of line, FileWriter \n

javaandroidappendnewlinefilewriter

提问by Anticipating

I'm trying to append a newline to an already existing file, but all commands I've tried have resulted in just appending to the first line. I want the output in the file to look like

我正在尝试将换行符附加到已经存在的文件中,但我尝试过的所有命令都导致仅附加到第一行。我希望文件中的输出看起来像

ROW1

第1行

ROW2

第2行

 private void addToFavToFile(String site){
        BufferedWriter writer = null;
        File root = Environment.getExternalStorageDirectory();
        try{
            writer = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/test2.txt",true));
            writer.write("ROW1");
            writer.newLine();
            writer.write("ROW2");
            writer.close();
        }catch(Exception e){

        }

How do I solve this?

我该如何解决这个问题?

回答by tajhamille

I have tried the following code and it seems to produce what you are after.

我已经尝试了以下代码,它似乎产生了你想要的东西。

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TestNewLines {

public static void main(String[] args) {
    TestNewLines testFiles = new TestNewLines();
    testFiles.addToFavToFile("");
}

private void addToFavToFile(String site){
    BufferedWriter writer = null;
    try{
        writer = new BufferedWriter(new FileWriter("test.txt",true));
        insertEmptyLine(writer);
        writer.write("ROW1: " + System.currentTimeMillis());
        insertEmptyLine(writer);
        writer.write("ROW2: " + System.currentTimeMillis());
        insertSeperator(writer);
        writer.close();
    } catch(Exception e){

    }
}

private void insertEmptyLine(BufferedWriter writer) throws IOException {
    writer.newLine();
    writer.newLine();
}

private void insertSeperator(BufferedWriter writer) throws IOException {
    writer.newLine();
    writer.write("********************");
}
}

To test it I put the line "This is the first line." at the start of my test file. After running it a few times, the output looks like this:

为了测试它,我放了一行“这是第一行”。在我的测试文件的开头。运行几次后,输出如下所示:

This is the first line.

ROW1: 1335189657127

ROW2: 1335189657127


ROW1: 1335189658390

ROW2: 1335189658390


ROW1: 1335189665738

ROW2: 1335189665738


ROW1: 1335189679638

ROW2: 1335189679638


这是第一行。

第 1 行:1335189657127

第 2 行:1335189657127


第 1 行:1335189658390

第 2 行:1335189658390


第 1 行:1335189665738

第 2 行:1335189665738


第 1 行:1335189679638

第 2 行:1335189679638


As you can see from the timestamps, I am appending new lines to the end of the file each time.

从时间戳中可以看出,我每次都在文件末尾添加新行。

回答by TechSpellBound

Instead of adding one new line, try adding two newlines in a row.

不要添加一个新行,而是尝试在一行中添加两个换行符。

One for going to the line below ROW1 and the other for inserting one more blank line.

一个用于转到 ROW1 下方的行,另一个用于再插入一个空行。

回答by Rahul Borkar

Please use following code,

请使用以下代码,

private void addToFavToFile(String site){
    BufferedWriter writer = null;
    File root = Environment.getExternalStorageDirectory();
    try{
        writer = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/test2.txt",true));
        writer.write("ROW1");
        writer.newLine();
        writer.write("ROW2");
        writer.newLine();
        writer.close();
    }catch(Exception e){

    }

回答by Shaikh Farooque

Instead of using writer.newLine();

而不是使用 writer.newLine();

You should use writer.write("\\n\\r");

你应该使用 writer.write("\\n\\r");

回答by Gijs Overvliet

You can also try to use PrintWriter.println(String s)

您也可以尝试使用 PrintWriter.println(String s)

http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html

回答by Mike Adler

If you just want the final line break, append the writer.newLine()after writer.write("Row2"). If there is no newLine being written, writer.newLine()may write the wrong line end marker into the file. Write it by hand.

如果您只想要最后的换行符,请writer.newLine()writer.write("Row2"). 如果没有写入 newLine,则writer.newLine()可能将错误的行结束标记写入文件。手写吧。

回答by wattostudios

Why don't you force it to write at the end of the file by including the offsetin the write commands...

为什么不通过offset在写入命令中包含 来强制它在文件末尾写入...

private void addToFavToFile(String site){
    BufferedWriter writer = null;
    File root = Environment.getExternalStorageDirectory();
    try{
        File fileToWrite = new File(Environment.getExternalStorageDirectory()+"/test2.txt");
        int endOfFile = (int)file.length();

        writer = new BufferedWriter(new FileWriter(fileToWrite,true));

        String string1 = "ROW1";
        writer.write(File.separator + string1,endOfFile,string1.length);
        endOfFile += string1.length;

        String string2 = "ROW2";
        writer.write(File.separator + string2,endOfFile,string2.length);
        endOfFile += string2.length;

        writer.close();
    }catch(Exception e){

    }