like this
Code:
import java.io.*;
public class FileStuff {
String filePath=null;
public FileStuff() {}
public void setFile(String filePath) {
this.filePath=filePath;
}
public void addLine(String aLine) {
StringBuffer sb=new StringBuffer();
try {
BufferedReader br=new BufferedReader(new FileReader(filePath));
String line=null;
while ((line=br.readLine())!=null) {
sb.append(line).append("\n");
}
br.close();
sb.append(aLine);
FileOutputStream fOut=new FileOutputStream(filePath);
fOut.write(sb.toString().getBytes());
fOut.close();
} catch (IOException ioe) {
System.err.println("File append for: ["+filePath+"] failed");
}
}
public static void main(String[] args) {
FileStuff fs=new FileStuff();
fs.setFile("c:\\somefile.txt");
fs.addLine("this is what i need to add to the file");
}
}
Bookmarks