Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not appending string to the file

Status
Not open for further replies.

mgl70

Programmer
Sep 10, 2003
105
US
Hi ,

I am getting trouble by using FileWriter.
I created a file by using FileWriter class.
I called the file from some other class.
In the second class, I am able to write a string to that file.
I application is still active. But I am not able to append another string to that file eventhough I gave "true".
Whats my problem.

Class1{

void method1(){

FileWriter fr = new FileWriter(filename,true);

}

}

Class2{

void method2()
{
String mystring;
fr.write(mystring);
fr.close();



}
here I am able to write the string to that file. but next time it is suposed to merge the second string to that file. It is not merging.

I appreatiate all your help,
 
Try something a bit more like :

Code:
public class Test {
	class Test2 {

			FileWriter fw = null;

			public Test2() {
				try {
					fw = new FileWriter("test.txt", true);
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}

			public void write(String s) throws IOException {
				fw.write(s);
				fw.flush();
			}

			public void close() throws IOException {
				fw.close();
				fw = null;
			}

	}

	public void doIt() throws IOException {
		Test2 t2 = new Test2();
		t2.write("hello");
		t2.write("\n");
		t2.write("there");
		t2.write("\n");
		t2.close();
	}


 	public static void main(String args[]) throws Exception {

		new Test().doIt();
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top