Hi i am facing problem in appending characters to the existing file. Also i need the difference between flush() and close().? As i read one of the post and i have tried without using flush() characters are written to the file or bytestream . ? how is that working?then what is the exact of flush method?
Hi Dittmer, But still i am not getting the expected output. Please correct me if i am wrong. try{ char[] arr={'c','a','b','d','e','f'}; File f=new File("fg.txt"); FileWriter fw=new FileWriter(f); fw.write('f'); fw.write(97); fw.write("arath",1,3); fw.write(arr,1,5); fw.close(); System.out.println(fw.getEncoding()); FileWriter fw1=new FileWriter(f,true);// Line 1fw1.write("vana",1,3); fw1.flush(); fw1.close();
I tried your code and I'm getting "faratabdefana". What's your output ? By the way, the strings you are working with aren't very helpfull in such discussions. Something like fw.write("fw"), fw1.write("fw1") would be much more descriptive than fw.write("arath",1,3) or fw.write(arr,1,5).
Sazzad Hossain
Greenhorn
Joined: Jul 21, 2008
Posts: 10
posted
0
Hi, Just adding the line fw.flush() before opening the second FileWriter gave me the expected output.
try{ char[] arr = {'c','a','b','d','e','f'}; File f = new File("C:\\_develop\\Test.txt"); FileWriter fw = new FileWriter(f); fw.write('f'); fw.write(97); fw.write("arath",1,3); fw.write(arr,1,5); fw.flush(); // Add this line System.out.println(fw.getEncoding()); FileWriter fw1=new FileWriter(f,true); fw1.write("vana",1,3); fw1.flush(); fw1.close(); fw.close(); } catch(IOException ioe) { ioe.printStackTrace(); } }
}
Hope this help.
Raphael Rabadan
Ranch Hand
Joined: Jul 05, 2008
Posts: 141
posted
0
Hello, when we use FileWriter it has a some kind of buffer so we don't use so much the I/O to get more performance. So, when we use .flush() we make sure to write the things we used on .write() commands on the file.
To say the truth i don't know how exactly the buffers works, but, to make sure, use the .flush() to write the things :-)
and guys, please use the CODE tag for codes. here is a code that works how it should: