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

