Online-Academy
Look, Read, Understand, Apply

Streams

ByteStream and CharacterStream

Byte Stream - Writing to a File

For byte streams, we use FileOutputStream

import java.io.FileOutputStream;
import java.io.IOException;

public class ByteWrite {
    public static void main(String[] args) {

        try {
            FileOutputStream out =
                new FileOutputStream("bytefile.txt");
            String data = "Hello Java";
            byte[] bytes = data.getBytes();
//It converts the string into a sequence of bytes.
            out.write(bytes);
            out.close();
            System.out.println("Data written successfully.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}    

For Reading bytes, we use FileInputStream

import java.io.FileInputStream;
import java.io.IOException;

public class ByteRead {
    public static void main(String[] args) {
        try {
            FileInputStream in =
                new FileInputStream("bytefile.txt");
            int data;
            while ((data = in.read()) != -1) {
            //Keep reading until there are no more bytes.
            //reads one byte at a time.
            //The value -1 means that the end of the file has been reached.
                System.out.print((char) data);
            //only because our file contains text and we want to display the byte as a character.
            }
            in.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Character Stream - Writing to a File

For character streams, we can use FileWriter

import java.io.FileWriter;
import java.io.IOException;

public class CharacterWrite {
    public static void main(String[] args) {

        try {
            FileWriter writer =
            new FileWriter("characterfile.txt");
            writer.write("Hello Java");
            writer.write("\n");
            writer.write("This is a character stream.");
            writer.close();
            System.out.println("Data written successfully.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Character Stream - Reading From a File

For reading characters, we can use FileReader

import java.io.FileReader;
import java.io.IOException;

public class CharacterRead {
    public static void main(String[] args) {
        try {
            FileReader reader =
                new FileReader("characterfile.txt");
            int data;
            while ((data = reader.read()) != -1) {
                System.out.print((char) data);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Important Difference in the Programs

Byte Stream

  • FileOutputStream
  • FileInputStream
  • Character Stream

  • FileWriter
  • FileReader
  • Copying an Image: example

    Here, byte stream is used not a character stream

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class CopyImage {
        public static void main(String[] args) {
            try {
                FileInputStream in =
                    new FileInputStream("photo.jpg");
                FileOutputStream out =
                    new FileOutputStream("copy.jpg");
                int data;
                while ((data = in.read()) != -1) {
                    out.write(data);
                }
                in.close();
                out.close();
                System.out.println("Image copied successfully.");
            } catch (IOException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
    

    Note: Java needs a special value to indicate end of file A char cannot represent -1, so the method returns an int. After checking for EOF, the value is treated as a character.

    Simple Rule to Remember

  • Byte Stream -> InputStream / OutputStream -> raw Bytes -> binary files
  • Character Stream -> Reader /Writer -> characters/text -> text files