Java BufferedWriter isn't working -


im having problem bufferedwriter. reading in 50,000 word wordlist, using stemming algorithm , creating new wordlist contains word stems. instead of new file containing stems litrally contains:

-

here code:

public static void main(string[] args) {     bufferedreader reader=null;     bufferedwriter writer=null;     try {         writer = new bufferedwriter(new filewriter(new file("src/newwordlist.txt")));         hashset<string> db = new hashset<string>();         reader = new bufferedreader(new inputstreamreader(new fileinputstream("src/wordlist"),"utf-8"));         string word;         int i=0;         while ((word=reader.readline())!=null) {             i++;             stemmer s= new stemmer();             s.addword(word);             s.stem();             string stem =s.tostring();             if(!db.contains(stem)){                 db.add(stem);                 writer.write(stem);                 //system.out.println(stem);             }         }         system.out.println("reduced file " + + " words " + db.size());         reader.close();         writer.close();     } catch (ioexception e1) {         e1.printstacktrace();     } } 

the output on console is:

reduced file 58110 words 28201

so know working. ive tried changing writer.write(stem); writer.write("hi"); , still same output in newwordlist.txt. know no fault of stemmer class, ive tried outputting stem string (where commented code) , produced correct output console fault must writer dont understand what.


edit 1

i simplified code to:

        bufferedreader reader=null;         bufferedwriter writer=null;         try {             writer = new bufferedwriter(new filewriter(new file("src/newwordlist.txt")));             hashset<string> db = new hashset<string>();             reader = new bufferedreader(new inputstreamreader(new fileinputstream("src/wordlist.txt"),"utf-8"));             string word;             int i=0;             while ((word=reader.readline())!=null) {                 i++;                 if(!db.contains(word)){                     db.add(word);                     writer.write("hi");                 }             }             system.out.println("reduced file " + + " words " + db.size());             reader.close();             writer.close();         } catch (ioexception e1) {             e1.printstacktrace();         } 

now console output:

reduced file 58110 words 58109 

but output file still blank

i expect code given in question produce file consists of 1 line, consisting of of "stems" concatenated. (or in "hi" version, 1 line consisting of "hihihi...." repeated large number of times.)

it conceivable whatever using view file cannot cope input file consists of many thousands of characters ... , no end-of-line.

change

    writer.write(stem); 

to

    writer.write(stem);     writer.write(eol); 

where eol platform specific end-of-line sequence.


assuming using java 7, better use try-with-resource make sure output stream closed / flushed, if there error:

public static void main(string[] args) {     try (bufferedreader reader = new bufferedreader(              new inputstreamreader(new fileinputstream("src/wordlist"), "utf-8"));          bufferedwriter writer = new bufferedwriter(new filewriter(              new file("src/newwordlist.txt")));         hashset<string> db = new hashset<>();         string eol = system.getproperty("line.separator");         string word;         int = 0;         while ((word = reader.readline()) != null) {             i++;             stemmer s = new stemmer();             s.addword(word);             s.stem();             string stem = s.tostring();             if (db.add(stem)) {                 writer.write(stem);                 writer.write(eol);             }         }         system.out.println("reduced file " + + " words " + db.size());     } catch (ioexception e1) {         e1.printstacktrace();     } } 

(i tidied couple of other things ...)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -