Java 为什么 bufferedwriter 没有写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4430143/
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
why is bufferedwriter not writing in the file?
提问by Maverick
Here is the code snippet.
这是代码片段。
read = new FileReader("trainfiles/"+filenames[i]);
br = new BufferedReader(read);
while((lines = br.readLine())!=null){
st = new StringTokenizer(lines);
while(st.hasMoreTokens()){
bw = new BufferedWriter(new FileWriter("files/file.txt"));
bw.write(st.nextToken());
bw.newLine();
}
}
Edit: I am reading files from a directory. So, I need to open the reader in every loop. I have made some modification, but then also it is not writing to that file. Here is the code:
编辑:我正在从目录中读取文件。所以,我需要在每个循环中打开阅读器。我做了一些修改,但它也没有写入该文件。这是代码:
for(i=0;i==0;i++){
if(filenames[i].matches(".*ham.*")){
System.out.println("ham:"+filenames[i]);
read = new FileReader("trainfiles/"+filenames[i]);
br = new BufferedReader(read);
while((lines = br.readLine())!=null){
st = new StringTokenizer(lines);
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
bw.write(st.nextToken());
}
}
bw.close();
br.close();
}else{
System.out.println("spam:"+filenames[i]);
}
}
edit: I modified the code, but no success,
编辑:我修改了代码,但没有成功,
while((lines = br.readLine())!=null){
st = new StringTokenizer(lines);
bw = new BufferedWriter(new FileWriter("files/file.txt"));
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
bw.write(st.nextToken());
}
bw.close();
}
br.close();
And i am getting this error: Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at Test.main(Test.java:30)
我收到此错误: Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at Test.main(Test.java:30)
edit: Thanks guys.. I figured it out. Actually I created an directory in eclipse and I did not refresh it to see the content. Its silly... anyways.thanks a lot
编辑:谢谢你们..我想通了。其实我在eclipse中创建了一个目录,我没有刷新它来查看内容。它很愚蠢......反正。非常感谢
采纳答案by Tiago
- You are creating the FileWritter inside the loop so you will always truncate the file in each cycle.
- You forgot to close / flush the writter
- But with some luck (terminating the program may cause the writter to flush) the file would contain the last word of your input file which I can only guess would be a new line and you probably missed when you opened up the file to check the content.
- 您正在循环内创建 FileWritter,因此您将始终在每个循环中截断文件。
- 你忘记关闭/刷新作者
- 但幸运的是(终止程序可能会导致作者刷新)该文件将包含您输入文件的最后一个字,我只能猜测这将是一个新行,当您打开文件检查内容时,您可能会错过.
Your inner loop should be something like this:
你的内部循环应该是这样的:
try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) {
while (st.hasMoreTokens()) {
bw.write(st.nextToken());
bw.newLine();
}
}
回答by Darron
A couple of things:
几件事:
- You're creating a new FileWriter each time through the loop, which will truncate the file each time.
- BufferedWriter is buffered, and you're never flushing the buffer. You need to either call bw.flush(), or even better, close the writer when done.
- 每次循环都会创建一个新的 FileWriter,它每次都会截断文件。
- BufferedWriter 是缓冲的,您永远不会刷新缓冲区。您需要调用 bw.flush(),或者更好的是,完成后关闭编写器。
回答by dhruven1600
Ideally you should use following Constructor to create FileWriter
,
理想情况下,您应该使用以下构造函数来创建FileWriter
,
bw = new BufferedWriter(new FileWriter("files/file.txt",true));
Second parameter, true
is for appending the new data. FileWriter
will not truncate your previous write.
第二个参数,true
用于追加新数据。FileWriter
不会截断您之前的写入。
and your reader will be able to read proper data.
并且您的读者将能够读取正确的数据。