Java File IO FileInputStream and FileOutputStream Examples

This Java File IO tutorial helps you understand and use the FileInputStream and FileOutputStream classes for manipulating binary files.

In Java, FileInputStream and FileOutputStream are byte streams that read and write data in binary format, exactly 8-bit bytes. They are descended from the abstract classes InputStream and OutputStream which are the super types of all byte streams.

The following class diagram helps you understand the API hierarchy of these byte stream classes:

ByteStreamsAPI

You can notice that these classes implement the AutoCloseable interface, which means that we can use the try-with-resources structure to close the streams automatically.

1. Understanding the FileInputStream Class

The FileInputStream is a byte input stream class that provides methods for reading bytes from a file. We can create an instance of this class by supplying a File or a path name, using these two constructors:

2. Understanding the FileOutputStream Clas

The FileOutputStream is a byte output stream class that provides methods for writing bytes to a file. We can create an instance of this class by supplying a File or a path name, and/or specify to overwrite or append to an existing file, using the following constructors:

Now, let’s see various code examples demonstrating the usages of the FileInputStream and FileOutputStream classes.

3. FileInputStream and FileOutputStream Examples

Example #1: Copy a File

The following program copies a file to another:
import java.io.*; /** * This program demonstrates how to copy a file using the * FileInputStream and FileOutputStream classes with a * byte array as a buffer. * @author www.codejava.net */ public class CopyFile < private static final int BUFFER_SIZE = 4096; public static void main(String[] args) < if (args.length < 2) < System.out.println("Please provide input and output files"); System.exit(0); >String inputFile = args[0]; String outputFile = args[1]; try ( InputStream inputStream = new FileInputStream(inputFile); OutputStream outputStream = new FileOutputStream(outputFile); ) < byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) < outputStream.write(buffer, 0, bytesRead); >> catch (IOException ex) < ex.printStackTrace(); >> >
Run this program like this: java CopyFile For example:
java CopyFile ByteStreamsAPI.png D:\ByteStreamsAPI.png
We can also copy a file using FileChannel in NIO like this:
import java.io.*; import java.nio.channels.*; /** * This program demonstrates how to copy a file using the * FileInputStream and FileOutputStream classes with FileChannel.s * @author www.codejava.net */ public class CopyFileChannel < public static void main(String[] args) < if (args.length < 2) < System.out.println("Please provide input and output files"); System.exit(0); >String inputFile = args[0]; String outputFile = args[1]; try ( FileChannel sourceChannel = new FileInputStream(inputFile).getChannel(); FileChannel destChannel = new FileOutputStream(outputFile).getChannel(); ) < sourceChannel.transferTo(0, sourceChannel.size(), destChannel); >catch (IOException ex) < ex.printStackTrace(); >> >

Example #2: Combine 2 files

The following program combines two files to the 3 rd file:
import java.io.*; /** * This program demonstrates how to combine two files to a 3rd file * using the FileInputStream and FileOutputStream classes. * @author www.codejava.net */ public class CombineFile < private static final int BUFFER_SIZE = 4096; public static void main(String[] args) < if (args.length < 3) < System.out.println("Please provide file1, file2, and output files"); System.exit(0); >String inputFile1 = args[0]; String inputFile2 = args[1]; String outputFile = args[2]; try ( InputStream inputStream1 = new FileInputStream(inputFile1); InputStream inputStream2 = new FileInputStream(inputFile2); OutputStream outputStream = new FileOutputStream(outputFile); ) < byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream1.read(buffer)) != -1) < outputStream.write(buffer, 0, bytesRead); >while ((bytesRead = inputStream2.read(buffer)) != -1) < outputStream.write(buffer, 0, bytesRead); >> catch (IOException ex) < ex.printStackTrace(); >> >
Run this program like this: java CombineFile For example:
java CombineFile Sat.txt Sun.txt Weekend.txt

Example #3: Check file signature

The following program reads the first 4 bytes of a given file to check if it is a PDF document or not, based on the PDF file signature:

import java.io.*; import java.util.*; /** * This program demonstrates how to use the FileInputStream class to check * if a given file is actual a PDF file or not. * @author www.codejava.net */ public class CheckPDFFile < public static void main(String[] args) < if (args.length < 1) < System.out.println("Please provide the input file"); System.exit(0); >String inputFile = args[0]; byte[] pdfSignature = ; try ( InputStream inputStream = new FileInputStream(inputFile); ) < byte[] header = new byte[4]; inputStream.read(header); if (Arrays.equals(header, pdfSignature)) < System.out.println("It's a Real PDF file"); >else < System.out.println("It's NOT a PDF file"); >> catch (IOException ex) < ex.printStackTrace(); >> >
Run this program like this: java CheckPDFFile For example:
java JavaProjects.pdf
It's a Real PDF file

Example #4: Read File metadata

The following program reads the width and height of a PNG image file, according to PNG file format specification:

import java.io.*; import java.util.*; import java.math.*; /** * This program demonstrates how to use the FileInputStream class to read * metadata of a PNG image file such as width and height. * @author www.codejava.net */ public class ReadPNGImage < public static void main(String[] args) < if (args.length < 1) < System.out.println("Please provide the input file"); System.exit(0); >String inputFile = args[0]; int[] pngSignature = ; byte[] ihdrSignature = ; try ( InputStream inputStream = new FileInputStream(inputFile); ) < int[] pngHeader = new int[8]; // read first 8 bytes as PNG file signature for (int i = 0; i < 8; i++) < pngHeader[i] = inputStream.read(); >if (!Arrays.equals(pngHeader, pngSignature)) < System.out.println("The file is NOT a PNG image"); System.exit(-1); >// skip next 4 bytes (chunk data length) inputStream.skip(4); // read next 4 bytes as IHDR header chunk byte[] ihdrHeader = new byte[4]; inputStream.read(ihdrHeader); if (!Arrays.equals(ihdrHeader, ihdrSignature)) < System.out.println("Invalid IHDR header"); System.exit(-1); >// read next 4 bytes as the width value byte[] bytes = new byte[4]; inputStream.read(bytes); int width = new BigInteger(bytes).intValue(); System.out.println("Width = " + width); // read next 4 bytes as the height value inputStream.read(bytes); int height = new BigInteger(bytes).intValue(); System.out.println("Height brush:text">java ReadPNGImage For example:
java ReadPNGImage ByteStreamsAPI.png
Width = 285 Height = 395

Example #5: Attach one file to another

The following program attaches content of a file to another:
import java.io.*; /** * This program demonstrates how to attach one file at the end of another file * using the FileInputStream and FileOutputStream classes * @author www.codejava.net */ public class AttachFile < private static final int BUFFER_SIZE = 4096; public static void main(String[] args) < if (args.length < 2) < System.out.println("Please provide file1 and file2"); System.exit(0); >String inputFile = args[0]; String outputFile = args[1]; try ( InputStream inputStream = new FileInputStream(inputFile); OutputStream outputStream = new FileOutputStream(outputFile, true); ) < byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) < outputStream.write(buffer, 0, bytesRead); >> catch (IOException ex) < ex.printStackTrace(); >> >
Run this program like this: java AttachFile For example:
java AttachFile ByteStreamsAPI.png MDS.exe
This attaches an image file to the end of a Windows executable (EXE) file.

API References:

Related Java File IO Tutorial:

Other Java File IO Tutorials:

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Add comment

Comments

#1 Fasikaw Kindye 2019-01-04 14:09 very good tutorial
I like the short notes

CodeJava.net delivers Java tutorials, code examples and sample projects for programmers at any level of expertise.
This site is founded and maintained by Nam Ha Minh - a highly passionate and skilled programmer.


Copyright © 2012 - 2024 CodeJava.net, all rights reserved.