为什么 bufferedwriter 不写入文件?
这是代码片段。
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();
}
}
编辑: 我正在从目录中读取文件。所以,我需要在每个循环中打开阅读器。我做了一些修改,但它也没有写入该文件。 这是代码:
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]);
}
}
编辑: 我修改了代码,但没有成功,
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();
并且收到此错误: Thread "main" java.util.NoSuchElementException 中出现异常 在 java.util.StringTokenizer.nextToken(StringTokenizer.java:332) 在 Test.main(Test.java:30)
编辑: 谢谢大家..我明白了。实际上我在eclipse中创建了一个目录,并且没有刷新它来查看内容。无论如何,这很愚蠢……非常感谢
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几件事:
A couple of things:
你的内循环应该是这样的:
Your inner loop should be something like this:
理想情况下,您应该使用以下构造函数来创建
FileWriter
,第二个参数
true
用于附加新数据。FileWriter
不会截断您之前的写入。您的读者将能够读取正确的数据。
Ideally you should use following Constructor to create
FileWriter
,Second parameter,
true
is for appending the new data.FileWriter
will not truncate your previous write.and your reader will be able to read proper data.