Add a string to a file?

I don found somethink to this topic so I will post my solution

i worte an extra class because the bufferedWriter do not have a add method.

so just add the code in a new class:

/**

* @(#)FileWriter.java

*

*

* @Max Pfeiffer

* @version 1.00 2007/2/7

*/

import java.io.*;

public class WriterToFile {

FileWriter fstream;

BufferedWriter out;

public WriterToFile(String tmp)

{

try{

fstream = new FileWriter(tmp);

out = new BufferedWriter(fstream);

}catch (Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}

}

public void add(String str)

{

try{

System.out.println("wite into File");

String tmp = new String(str+"\r\n ");

out.write(tmp);

}catch (Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}

}

public void close()

{

try{

System.out.println("close File");

out.close();

}catch (Exception e){//Catch exception if any

System.err.println("Error: close before open" +

e.getMessage());

}

}

}

To call the class:

WriterToFile file = new WriterToFile("rcodeRunPara.txt");

than uses each time if you want add somethink to the file:

file.add("hello");

It is nesseary to close the file wehn you are finshed:

file.close();

jonbob

null

[1511 byte] By [jonboba] at [2007-11-26 17:41:02]
# 1
Thanks for everythink.
DavidKNa at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 2
what?
qUesT_foR_knOwLeDgea at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 3
1. Why not just do the following, instead of using your class:bufferedWriter.write(string);bufferedWriter.newLine();(You code wasn't platform independent, by the way.)2. Yes, close an open file when you are done.
DrLaszloJamfa at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 4
sorry for the bad Englishok i will never post a solution!!!
jonboba at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 5

> sorry for the bad English

Thats ok.There are a lot of people here who hail from places where english isn't their native language

> ok i will never post a solution!!!

I still can't understand the Solution for what?Was it for some problem someone else previously posted or what?

qUesT_foR_knOwLeDgea at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 6

// FileWriter( filename, appendMode)

// set append mode true, if you want write at the bottom of the file.

PrintWriter pw = new PrintWriter( new FileWriter("hello.txt", true) );

pw.println( "print with line feed... Hello" );

pw.print( "print ... Hello" );

pw.close();

masuda1967a at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 7

> // FileWriter( filename, appendMode)

> // set append mode true, if you want write at the

> bottom of the file.

>

> PrintWriter pw = new PrintWriter( new

> FileWriter("hello.txt", true) );

> pw.println( "print with line feed... Hello" );

> pw.print( "print ... Hello" );

> pw.close();

? Who mentioned append mode?

DrLaszloJamfa at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 8

If you use append mode for PrintWriter,

no need to create WriterToFile Class.

WriterToFile file = new WriterToFile("rcodeRunPara.txt");

file.add("hello");

file.close();

// code avobe can be changed to below

// without using custom class WriteToFile

// or

// Is this assignment not allow to use append?

PrintWriter pw = new PrintWriter( new FileWriter("rcodeRunPara.txt", true) );

pw.println( "hello" );

pw.close();

masuda1967a at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...
# 9
Uncle!
DrLaszloJamfa at 2007-7-9 0:09:13 > top of Java-index,Java Essentials,New To Java...