FileOutputStream Tutorial

All of my I/O tutorials up until now have been dealing with Character Streams. In this tutorial I will begin to introduce you to writing Byte data to a file. The primary purpose of the FileOutputStream class is to write an output stream to a file. This tutorial will demonstrate the write(int b) method to create a BMP image file from scratch. By the end of this tutorial you will have created an 8x8 pixel striped-color bitmap image file. The FileOutputStream class implements AutoClosable so we can use the try-with-resources type exception handling. I will demonstrate the other overloaded versions of the write() method in my FileInputStream tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md FOS
C:\Java>cd FOS
C:\Java\FOS>Notepad FOS.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class FOS {
    public static void main(String args[]) {
        File f = new File(File.separator+"Java"+File.separator+"FOS"+File.separator+"test.bmp");
        f.delete();
        try (FileOutputStream fos = new FileOutputStream(f)) {
            fos.write(66); // ascii 'B' 
            fos.write(77); // ascii 'M' - BM is the file type
            fos.write(220); // Size of the file = Header (26) + (length * width * 3 = 192) + 2 closing null 0's
            fos.write(0); // Size of the file
            fos.write(0); // Size of the file
            fos.write(0); // Size of the file
            fos.write(0); // Reserved 1
            fos.write(0); // Reserved 1
            fos.write(0); // Reserved 2
            fos.write(0); // Reserved 2
            fos.write(26); // 4 Byte number representing Byte# for start of Pixel data
            fos.write(0); // 4 Byte number representing Byte# for start of Pixel data
            fos.write(0); // 4 Byte number representing Byte# for start of Pixel data
            fos.write(0); // 4 Byte number representing Byte# for start of Pixel data - End of File Header here
            fos.write(12); // 4 Byte number representing Image header size - Image header starts here
            fos.write(0); // 4 Byte number representing Image header size
            fos.write(0); // 4 Byte number representing Image header size
            fos.write(0); // 4 Byte number representing Image header size
            fos.write(8); // 2 Byte number representing Image width
            fos.write(0); // 2 Byte number representing Image width
            fos.write(8); // 2 Byte number representing Image height
            fos.write(0); // 2 Byte number representing Image height
            fos.write(1); // number of planes in the image
            fos.write(0); // number of planes in the image
            fos.write(24); // number of bits per pixel 24 = 3 bytes per pixel = RGB
            fos.write(0); // number of bits per pixel

            for (int i = 0; i < 8; i ++) {
                fos.write(255);
                fos.write(0);
                fos.write(0);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(0);
                fos.write(255);
                fos.write(0);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(0);
                fos.write(0);
                fos.write(255);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(127);
                fos.write(127);
                fos.write(127);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(180);
                fos.write(200);
                fos.write(47);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(200);
                fos.write(60);
                fos.write(199);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(32);
                fos.write(165);
                fos.write(100);			
            }
            for (int i = 0; i < 8; i ++) {
                fos.write(155);
                fos.write(10);
                fos.write(134);			
            }

            fos.write(0);
            fos.write(0);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Now switch back to the command prompt (CMD) and type in javac FOS.java and press Enter.
Now type in java FOS and press Enter.


C:\Java\FOS>javac FOS.java
C:\Java\FOS>java FOS
Watch video for more information


Final thoughts

None


Tutorials