FileOutputStream
In this chapter you will learn:
- How to use FileOutputStream
- Create FileOutputStream object from File object
- Append output to file using FileOutputStream
- How to copy a file with FileInputStream and FileOutputStream
- Write byte array to a file using FileOutputStream
Use FileOutputStream
FileOutputStream
has the following definition of
class FileOutputStream extends OutputStream
FileOutputStream
extends the OutputStream
.
java.lang.Object
|
+-java.io.OutputStream
|
+-java.io.FileOutputStream
A file output stream is an output stream for writing data to a File.
FileOutputStream
is useful for writing streams of raw bytes such as image data.
For writing streams of characters, consider using FileWriter.
FileOutputStream
creates an OutputStream that you can use to write bytes to a file.
If append is true, the file is opened in append mode.
Creation of a FileOutputStream
is not dependent on the file already existing.
FileOutputStream
will create the file before opening it for
output when you create the object.
Opening a read-only file will throw an IOException.
Create FileOutputStream object from File object
The following code shows how to
Create FileOutputStream
object from File
object
import java.io.File;
import java.io.FileOutputStream;
//from j av a 2s . co m
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("C:/demo.txt");
FileOutputStream fos = new FileOutputStream(file);
}
}
Append output to file using FileOutputStream
In order to append content to a file using FileOutputStream
we pass in true
as the second parameter.
import java.io.FileOutputStream;
/*from java 2 s . c o m*/
public class Main {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("C:/demo.txt", true);
fos.write("Appended".getBytes());
fos.close();
}
}
Copy Bytes between FileInputStream and FileOutputStream
FileInputStream and FileOutputStream can be used together to
copy a file. In the following code it creates FileInputStream and FileOutputStream from file
names in string. The use a while
loop to control the copy process.
The code checks each int read from the FileInputStream to see if
it is -1
. -1
means we hit the end of the file and should stop.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//from j a v a 2 s .com
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("in.txt");
out = new FileOutputStream("out.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Write byte array to a file using FileOutputStream
The following code converts a string value into
byte array using the default system encoding. Then
it calls the write
method from FileOutputStream
to persist the byte array into the underline file.
import java.io.FileOutputStream;
// j a va 2 s.c o m
public class Main {
public static void main(String[] args) throws Exception {
String strFilePath = "C://demo.txt";
FileOutputStream fos = new FileOutputStream(strFilePath);
String strContent = "Write File using Java FileOutputStream example from java2s.com!";
fos.write(strContent.getBytes());
fos.close();
}
}
Next chapter...
What you will learn in the next chapter: