Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java - why is bufferedwriter not writing in the file?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...