为什么 bufferedwriter 不写入文件?

发布于 2024-10-07 01:52:00 字数 2047 浏览 7 评论 0原文

这是代码片段。

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 技术交流群。

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

发布评论

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

评论(3

一梦浮鱼 2024-10-14 01:52:00

有几件事:

  1. 您每次通过循环都会创建一个新的 FileWriter,这每次都会截断文件。
  2. BufferedWriter 是缓冲的,并且您永远不会刷新缓冲区。您需要调用 bw.flush(),或者更好的是,完成后关闭编写器。

A couple of things:

  1. You're creating a new FileWriter each time through the loop, which will truncate the file each time.
  2. 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.
酷炫老祖宗 2024-10-14 01:52:00
  • 您正在循环内创建 FileWritter,因此您将始终在每个循环中截断文件。
  • 您忘记关闭/刷新写入器
  • 但是运气好的话(终止程序可能会导致写入器刷新)该文件将包含输入文件的最后一个单词,我只能猜测这将是一个新行,当您打开文件检查内容。

你的内循环应该是这样的:

try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) {
    while (st.hasMoreTokens()) {
        bw.write(st.nextToken());
        bw.newLine();
    }
}
  • 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.

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();
    }
}
又怨 2024-10-14 01:52:00

理想情况下,您应该使用以下构造函数来创建 FileWriter

bw = new BufferedWriter(new FileWriter("files/file.txt",true));

第二个参数 true 用于附加新数据。 FileWriter 不会截断您之前的写入。

您的读者将能够读取正确的数据。

Ideally you should use following Constructor to create 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.

and your reader will be able to read proper data.

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