Android 不会在文本文件中写入新行

发布于 2025-01-06 04:57:52 字数 1094 浏览 1 评论 0原文

我正在尝试在 android 中的文本文件中写入新行。

这是我的代码:

FileOutputStream fOut;
try {
    String newline = "\r\n";
    fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(data);
    osw.write(newline);

    osw.flush();
    osw.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

我尝试过 \n\r\n 并且我也尝试获取新行的系统属性,但它们都不起作用。

数据变量包含来自同一文件的先前数据。

String data = "";

try {
    FileInputStream in = openFileInput("cache.txt");   
    StringBuffer inLine = new StringBuffer();
    InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
    BufferedReader inRd = new BufferedReader(isr,8 * 1024);
    String text;

    while ((text = inRd.readLine()) != null) {
        inLine.append(text);
    }

    in.close();
    data = inLine.toString();
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

I am trying to write a new line to a text file in android.

Here is my code:

FileOutputStream fOut;
try {
    String newline = "\r\n";
    fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(data);
    osw.write(newline);

    osw.flush();
    osw.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

I have tried \n, \r\n and I did also try to get the system property for a new line, neither of them work.

The data variable contains previously data from the same file.

String data = "";

try {
    FileInputStream in = openFileInput("cache.txt");   
    StringBuffer inLine = new StringBuffer();
    InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
    BufferedReader inRd = new BufferedReader(isr,8 * 1024);
    String text;

    while ((text = inRd.readLine()) != null) {
        inLine.append(text);
    }

    in.close();
    data = inLine.toString();
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

dawn曙光 2025-01-13 04:57:52

我也遇到了同样的问题,尝试了书中的每一个技巧。

我的问题:换行符已写入,但在读取时被删除:

while (readString != null) {
                datax.append(readString);
                readString = buffreader.readLine();
            }

文件是逐行读取并连接的,因此换行符消失了。

我没有在记事本或其他东西中查看原始文件,因为我不知道在手机上查看哪里,并且我的日志屏幕使用了删除换行符的代码:-(

所以简单的方法是在阅读时将其放回去:

while (readString != null) {
                datax.append(readString);
                datax.append("\n");
                readString = buffreader.readLine();
            }

I had the same problems, tried every trick in the book.

My problem: the newline's were written, but while reading they were removed:

while (readString != null) {
                datax.append(readString);
                readString = buffreader.readLine();
            }

The file was read line by line and concatenated, so the newline's disappeared.

I did not look at the original file in Notepad or something because I didn't know where to look on my phone, and my logscreen used the code which removed the newline's :-(

So the simple soultion was to put it back while reading:

while (readString != null) {
                datax.append(readString);
                datax.append("\n");
                readString = buffreader.readLine();
            }
涙—继续流 2025-01-13 04:57:52

我执行了一个类似的程序,它对我有用。但我观察到了一种奇怪的行为。它将这些新行添加到文件中,但光标仍保留在第一行。如果您想验证,请在换行符后写入一个 String,您将看到该 String 写在这些新行的正下方。

I executed a similar program and it worked for me. I observed a strange behavior though. It added those new lines to the file, however the cursor remained at the first line. If you want to verify, write a String after your newline characters, you will see that the String is written just below those new lines.

苦行僧 2025-01-13 04:57:52

我遇到了同样的问题,无法编写换行符。相反,我使用 BufferdWritter 将新行写入文件,它对我有用。
这是一个示例代码片段:

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("cache.txt",0));
BufferedWriter bwriter = new BufferedWriter(out);
// write the contents to the file
bwriter.write("Input String"); //Enter the string here
bwriter.newLine();

I was having the same problem and was unable to write a newline. Instead I use BufferdWritter to write a new line into the file and it works for me.
Here is a sample code sniplet:

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("cache.txt",0));
BufferedWriter bwriter = new BufferedWriter(out);
// write the contents to the file
bwriter.write("Input String"); //Enter the string here
bwriter.newLine();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文