Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes FileWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "FileWriter" Watch "FileWriter" New topic
Author

FileWriter

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
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?

import java.io.*;

class TestFileWriter {

public static void main(String[] args) {

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);
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();
}


}
output:faharabdef

what my expected output:faratabdefana

Regards
Kirba
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
You should always close a file before opening a second FileWriter (or similar) that uses it.


Ping & DNS - my free Android networking tools app
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
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();


}catch(IOException ioe){
ioe.printStackTrace();
}

See the bold statement ,what i am trying to do is appending some characters to the contents in the file

Regards
Kirba
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Sure you get it. Just remove the comment:

// Line 1 fw1.write("vana",1,3);

But the first version of your program is really very interesting. Hope I will going to understand what happens there.


SCJP 5 (98%) - SCBCD 5 (98%)
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Hi Ralph,
Even after removing i am getting the same output,and not the expected outptu.

Please help....

Regards
Kirba.
kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
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 1
fw1.write("vana",1,3);
fw1.flush();
fw1.close();


}catch(IOException ioe){
ioe.printStackTrace();
}


not getting my expected output:faratabdefana
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
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
Hi,
Just adding the line fw.flush() before opening the second FileWriter gave me the expected output.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {

public static void main(String[] args) {

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
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:


Kind Regards,
Raphael Rabadan
[ July 22, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileWriter