文件在对象创建时自行清除
我遇到了一个似乎无法解决的烦人问题。我有一个名为 DirFormMgmt 的类,它管理两个文件;目录.txt 和格式.txt。问题是,当我在程序启动时实例化此类的实例时,它会清除启动前文件中的所有内容,因此,例如,如果我打开formats.txt 文件并输入“.avi”并运行程序文本文件将变为空白。这是我在类中运行的唯一代码来隔离问题,我不确定为什么会发生这种情况:
在类的顶部我声明:
File dirFile = new File("directories.txt");
File formatFile = new File("formats.txt");
private BufferedReader dirReader;
private BufferedWriter dirWriter;
private BufferedReader formatReader;
private BufferedWriter formatWriter;
构造函数:
public DirFormMgmt() throws IOException {
checkFileExistence();
initReaderWriter();
}
构造函数调用的两个方法:
public void checkFileExistence() throws IOException {
if (!dirFile.exists()) {
dirFile.createNewFile();
}
if (!formatFile.exists()) {
formatFile.createNewFile();
}
}
并且:
public void initReaderWriter() throws IOException {
dirReader = new BufferedReader(new FileReader(dirFile));
dirWriter = new BufferedWriter(new FileWriter(dirFile));
formatReader = new BufferedReader(new FileReader(formatFile));
formatWriter = new BufferedWriter(new FileWriter(formatFile));
}
我检查了看看 createNewFile() 方法是否被调用,但它们没有被调用,所以问题一定出在我的 initReaderWriter() 方法上,但我不确定出了什么问题,非常感谢任何帮助!
I am having an annoying problem that I cannot seem to fix. I have a class named DirFormMgmt which manages two files; directories.txt and formats.txt. The problem is that when I instantiate an instance of this class when the program starts it clears everything that was in the files before it started, so for instance if I open the formats.txt file and type in ".avi" and run the program the text file will become blank. Here is the only code I have running in my class to isolate the issue and I am unsure of why this is happening:
At the top of my class I declare:
File dirFile = new File("directories.txt");
File formatFile = new File("formats.txt");
private BufferedReader dirReader;
private BufferedWriter dirWriter;
private BufferedReader formatReader;
private BufferedWriter formatWriter;
The constructor:
public DirFormMgmt() throws IOException {
checkFileExistence();
initReaderWriter();
}
The two methods called by the constructor:
public void checkFileExistence() throws IOException {
if (!dirFile.exists()) {
dirFile.createNewFile();
}
if (!formatFile.exists()) {
formatFile.createNewFile();
}
}
and:
public void initReaderWriter() throws IOException {
dirReader = new BufferedReader(new FileReader(dirFile));
dirWriter = new BufferedWriter(new FileWriter(dirFile));
formatReader = new BufferedReader(new FileReader(formatFile));
formatWriter = new BufferedWriter(new FileWriter(formatFile));
}
I checked to see if the createNewFile() methods were being called but they were not, so the problem must be with my initReaderWriter() method, but I am unsure of what is wrong, any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
new FileWriter(file, true)
将内容附加到现有文件。否则,内容将被覆盖。Use
new FileWriter(file, true)
to append content to an existing file. Otherwise, the contents will be overwritten.您无法像这样读取和写入同一个文件。 FileWriter 构造函数用于写入文件并替换其所有内容。
读取内存中的所有内容,然后写入文件,或者写入另一个临时文件,完成后删除原始文件并重命名临时文件。
You can't read and write to the same file like this. The FileWriter constructor is used to write to a file and replace all its contents.
EIther read everything in memory, and then write to the file, or write to another temp file, and remove the original file and rename the temp file when you're done.