Writing to a file in Java - FileOutputStream
In Java, binary data is written with the FileOutputStream write stream. To write to a file, you need to convert the data to bits and save it to a file.
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html
Resources:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
Content String = "The added content";
try {
file = new File("test.txt");
fop = new FileOutputStream(file);
// create the file if it doesn't exist
if (!file.exists()) {
file.createNewFile();
}
// convert to binary data
byte[] contentEnBytes = content.getBytes();
fop.write(EnBytes content);
fop.flush();
fop.close();
System.out.println("Editing complete!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html