为什么每当我重新启动程序时,这个 data.txt 文件就会被删除?

发布于 2024-11-26 05:16:55 字数 940 浏览 2 评论 0原文

我有一个简单的 txt 文件,只能保存 1 个单词,但是每当我重新启动程序时,data.txt 中的所有内容都会被删除 - 我不知道为什么?

全类代码:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class InfoSaver {
    File data = new File("data.txt");
    FileOutputStream fos;
    PrintStream writer;
    FileInputStream fis;
    DataInputStream reader;
    public void init() throws IOException{
        fos = new FileOutputStream(data);
        writer = new PrintStream(fos);
        fis = new FileInputStream(data);
        reader = new DataInputStream(fis);

    }
    public void writeData(String info) {
        writer.println(info);

    }
    public String readData() throws IOException{
        return reader.readLine();
    }
    public void close() throws IOException{
        writer.close();
        reader.close();
    }
}

I have a simple txt file that will save only 1 word, but whenever I restart the program everything inside the data.txt is deleted - I don't know why?

The whole class code:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class InfoSaver {
    File data = new File("data.txt");
    FileOutputStream fos;
    PrintStream writer;
    FileInputStream fis;
    DataInputStream reader;
    public void init() throws IOException{
        fos = new FileOutputStream(data);
        writer = new PrintStream(fos);
        fis = new FileInputStream(data);
        reader = new DataInputStream(fis);

    }
    public void writeData(String info) {
        writer.println(info);

    }
    public String readData() throws IOException{
        return reader.readLine();
    }
    public void close() throws IOException{
        writer.close();
        reader.close();
    }
}

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

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

发布评论

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

评论(4

旧瑾黎汐 2024-12-03 05:16:55

要添加到现有文件而不是覆盖它,请使用 FileOutputStream 的构造函数,该构造函数允许您以附加模式打开它。

fos = new FileOutputStream(data, true);

To add to an existing file instead of overwriting it, use FileOutputStream's constructor that lets you open it in append mode.

fos = new FileOutputStream(data, true);
打小就很酷 2024-12-03 05:16:55

由于这一行:

 fos = new FileOutputStream(data);

此版本的 FileOutputStream 构造函数 将覆盖文件,但您可以使用 此版本

public FileOutputStream(File file,
                        boolean append)
                 throws FileNotFoundException

您必须通过将 append 字段设置为 true 来指定要附加到文件。

Because of this line:

 fos = new FileOutputStream(data);

This version of the constructor of FileOutputStream will overwite the file, but you could use this version:

public FileOutputStream(File file,
                        boolean append)
                 throws FileNotFoundException

You'd have to specify that you want to append to the file by setting the append field to true.

沧桑㈠ 2024-12-03 05:16:55

您不是在文件中附加新信息,而是覆盖它。

任何时候你只要在

fos = new FileOutputStream(数据);

命令,它会被清空,无论您是否在其中保存了任何内容。

You're not appending new information to the file, you're overwriting it.

Anytime you just open it in the

fos = new FileOutputStream(data);

command, it gets emptied, no matter if you have saved anything inside it or not.

很酷不放纵 2024-12-03 05:16:55

您的 FileOutputStream 正在覆盖您的文件。如果您想附加到文件末尾,您需要指定:

fos = new FileOutputStream(data, true);

当您遇到意外行为时,最好检查 API 以确保您调用的函数正在执行您期望的操作。 以下是 FileOutputStream 构造函数的 API

Your FileOutputStream is overwriting your file. If you want to append to the end of the file you need to specify that:

fos = new FileOutputStream(data, true);

When you encounter unexpected behavior it's a good idea to check the API to ensure the functions you're calling are doing what you expect. Here is the API for the FileOutputStream constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文