java 出于某种原因使用 FileWriter 和 BufferedWriter 清除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17244713/
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
Using FileWriter and BufferedWriter clearing file for some reason?
提问by None None
For some reason, when I create a new BufferedWriter and FileWriter in my program (Even if I haven't used it to write anything yet), it clears the file I have selected of all it's text.
出于某种原因,当我在我的程序中创建一个新的 BufferedWriter 和 FileWriter 时(即使我还没有用它来写任何东西),它会清除我选择的文件中的所有文本。
selectedFile is determined by a JFileChooser.
selectedFile 由 JFileChooser 确定。
public static File selectedFile;
public static void Encrypt() throws Exception {
try {
//if I comment these two writers out the file is not cleared.
FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
System.out.println(AESencrp.encrypt(line));
/*file is cleared regardless of whether or not these are commented out or
* not, as long as I create the new FileWriter and BufferedWriter the file
* is cleared regardless.*/
//bw.write(AESencrp.encrypt(line));
//bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
AESencrp.encrypt is just an encrypting class I have, it won't affect it. If I create a new FileWriter and BufferedWriter then this loop isn't even run (At least I do not believe so, as I don't get the encryption of line or the original contents of the file printed, which prints if I haven't created the new FileWriter/BufferedWriter.)
AESencrp.encrypt 只是我拥有的一个加密类,它不会影响它。如果我创建了一个新的 FileWriter 和 BufferedWriter,那么这个循环甚至不会运行(至少我不这么认为,因为我没有得到行的加密或打印文件的原始内容,如果我没有就打印出来) t 创建了新的 FileWriter/BufferedWriter。)
for (String line : lines) {
System.out.println(line);
System.out.println(AESencrp.encrypt(line));
/*file is cleared regardless of whether or not these are commented out or
* not, as long as I create the new FileWriter and BufferedWriter the file
* is cleared regardless.*/
//bw.write(AESencrp.encrypt(line));
//bw.close();
}
采纳答案by fge
This is because the constructor of FileWriter
you are using truncates the file if it already exists.
这是因为FileWriter
您正在使用的构造函数会截断文件(如果它已经存在)。
If you want to append the data instead, use:
如果要改为附加数据,请使用:
new FileWriter(theFile, true);
回答by rgettman
It sounds like you want to append to the file, and not overwrite it. Use the proper FileWriter
constructor that takes a boolean
on whether to append.
听起来您想附加到文件中,而不是覆盖它。使用合适的FileWriter
构造函数来boolean
确定是否要追加。
FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);
The constructor you are using, without a boolean
, defaults to "overwrite" mode. As soon as you create the FileWriter
in overwrite mode, it clears the file so it can start writing from the beginning.
您正在使用的构造函数,没有boolean
,默认为“覆盖”模式。FileWriter
在覆盖模式下创建后,它会立即清除文件,以便它可以从头开始写入。
Passing true
as the second argument will allow you to append instead of overwrite.
true
作为第二个参数传递将允许您追加而不是覆盖。
回答by AlexWien
you open it for write (for appending use the constructor with append option). what do you expect what else should happen?
Further your outcommented close()
is at the wrong place. it should be outside the loop.
您打开它进行写入(用于附加使用带有附加选项的构造函数)。你还期待什么?此外,您的评论close()
在错误的地方。它应该在循环之外。