• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileWriter

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should always close a file before opening a second FileWriter (or similar) that uses it.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ralph,
Even after removing i am getting the same output,and not the expected outptu.

Please help....

Regards
Kirba.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic