Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, file handling involves reading from and writing to files. This can be done using byte streams and character streams, each suited for different types of data.

Byte Streams

Byte streams are used to handle raw binary data. They are particularly useful for handling files that contain non-text data like images, audio, and video files.

Key Classes

  • FileInputStream: Reads raw bytes from a file.
  • FileOutputStream: Writes raw bytes to a file.

Example: Reading and Writing a Binary File

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

public class ByteStreamExample {
    public static void main(String[] args) {
        // Reading from a binary file
        try (FileInputStream fis = new FileInputStream("input.bin")) {
            int byteContent;
            while ((byteContent = fis.read()) != -1) {
                System.out.print((char) byteContent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Writing to a binary file
        try (FileOutputStream fos = new FileOutputStream("output.bin")) {
            String data = "Hello, this is a test.";
            fos.write(data.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

				
			

Character Streams

Character streams are used to handle data in the form of characters. They are designed for handling text data and are aware of character encoding, making them suitable for reading and writing text files.

Key Classes

  • FileReader: Reads characters from a file.
  • FileWriter: Writes characters to a file.
  • BufferedReader: Buffers the input, providing efficient reading of characters, arrays, and lines.
  • BufferedWriter: Buffers the output, providing efficient writing of characters, arrays, and lines.

Example: Reading and Writing a Text File

				
					import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStreamExample {
    public static void main(String[] args) {
        // Reading from a text file
        try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Writing to a text file
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
            String data = "Hello, this is a test.";
            bw.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

				
			

Differences between Byte Streams and Character Streams

  • Data Handling: Byte streams handle raw binary data, while character streams handle character data with appropriate encoding.
  • Classes: Byte streams use InputStream and OutputStream classes, whereas character streams use Reader and Writer classes.
  • Use Cases: Byte streams are suitable for binary files (like images, audio, and video), while character streams are suitable for text files.

Understanding the distinction between byte streams and character streams is crucial for efficient file handling in Java. Byte streams are best for binary data, and character streams are ideal for text data, ensuring proper encoding and decoding of characters. By choosing the appropriate stream type, you can handle different file types effectively in your Java applications.

Scroll to Top