0

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

第 1 行

行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){

        }

我该如何解决这个问题?

4

7 回答 7

1

我已经尝试了以下代码,它似乎产生了你所追求的。

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("********************");
}
}

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

这是第一行。

第1行:1335189657127

第2行:1335189657127


第1行:1335189658390

第2行:1335189658390


第1行:1335189665738

第2行:1335189665738


第1行:1335189679638

第2行:1335189679638


正如您从时间戳中看到的那样,我每次都将新行附加到文件的末尾。

于 2012-04-23T14:07:57.430 回答
0

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.

于 2012-04-23T17:11:52.593 回答
0

请使用以下代码,

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){

    }
于 2012-04-23T12:51:05.210 回答
0

而不是使用writer.newLine();

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

于 2012-04-23T12:53:36.187 回答
0

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

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

于 2012-04-23T12:55:42.137 回答
0

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

于 2012-04-23T12:58:14.670 回答
0

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){

    }
于 2012-04-23T12:59:09.380 回答