0% found this document useful (0 votes)
0 views

javaio

Java I/O (Input/Output) allows reading from input sources and writing to output destinations using two main types of streams: byte streams for binary data and character streams for text data. The document provides examples of various stream classes, including FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedReader, BufferedWriter, ObjectOutputStream, and ObjectInputStream, along with a project example that demonstrates user input handling and file operations. It also outlines how to set up and run a simple Java project for file I/O operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

javaio

Java I/O (Input/Output) allows reading from input sources and writing to output destinations using two main types of streams: byte streams for binary data and character streams for text data. The document provides examples of various stream classes, including FileInputStream, FileOutputStream, FileReader, FileWriter, BufferedReader, BufferedWriter, ObjectOutputStream, and ObjectInputStream, along with a project example that demonstrates user input handling and file operations. It also outlines how to set up and run a simple Java project for file I/O operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

What Are Java I/O Streams?

Java I/O (Input/Output) is used to read data from input sources (like files, keyboard, network)
and write data to output destinations (like files, console, network).

There are two types of streams:

Stream Type Base Class Data Type


Byte Streams InputStream / OutputStream Binary data (images, files)
Character Streams Reader / Writer Text data (characters)

Byte Streams in Java


1. FileInputStream – Read bytes from a file
import java.io.FileInputStream;
import java.io.IOException;

public class ByteReadExample {


public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input.txt")) {
int ch;
while ((ch = fis.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. FileOutputStream – Write bytes to a file


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

public class ByteWriteExample {


public static void main(String[] args) {
String data = "Hello, Byte Stream!";
try (FileOutputStream fos = new FileOutputStream("output.txt")) {
fos.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}

Character Streams in Java


3. FileReader – Read characters from a file
import java.io.FileReader;
import java.io.IOException;

public class CharReadExample {


public static void main(String[] args) {
try (FileReader reader = new FileReader("input.txt")) {
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

4. FileWriter – Write characters to a file


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

public class CharWriteExample {


public static void main(String[] args) {
String data = "Hello, Character Stream!";
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Buffered Streams (More Efficient)


Buffered streams read/write larger chunks of data, making I/O faster.

5. BufferedReader & BufferedWriter


import java.io.*;

public class BufferedReaderWriterExample {


public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new
FileWriter("buffered.txt"))) {
writer.write("Buffered writing example");
} catch (IOException e) {
e.printStackTrace();
}

try (BufferedReader reader = new BufferedReader(new


FileReader("buffered.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Object Streams (Serialization)


Use Object Streams to write and read entire Java objects.

6. ObjectOutputStream & ObjectInputStream


import java.io.*;

class Student implements Serializable {


int id;
String name;

Student(int id, String name) {


this.id = id;
this.name = name;
}
}

public class ObjectStreamExample {


public static void main(String[] args) {
Student s1 = new Student(101, "John");

// Serialize object
try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("student.ser"))) {
oos.writeObject(s1);
} catch (IOException e) {
e.printStackTrace();
}

// Deserialize object
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("student.ser"))) {
Student s2 = (Student) ois.readObject();
System.out.println("ID: " + s2.id + ", Name: " + s2.name);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Standard Input/Output Streams


7. Reading from Console using System.in
import java.io.*;

public class ConsoleInputExample {


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter something: ");
String input = br.readLine();
System.out.println("You entered: " + input);
}
}

Summary Table
Task Class Used
Read binary file FileInputStream
Write binary file FileOutputStream
Read text file FileReader / BufferedReader
Write text file FileWriter / BufferedWriter
Serialize object ObjectOutputStream
Deserialize object ObjectInputStream
Read from console InputStreamReader + BufferedReader

Project Name: File I/O Stream Example in Java

 Take user input (name, age, etc.)


 Save the data to a file
 Read the file content
 Display it to the user

Project Structure
FileIOProject/

├── Main.java
└── user_data.txt

Main.java

import java.io.*;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
String fileName = "user_data.txt";

Scanner scanner = new Scanner(System.in);


try {
// 1. Get user input
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Enter your age:");


String age = scanner.nextLine();

// 2. Write data to file using BufferedWriter


BufferedWriter writer = new BufferedWriter(new
FileWriter(fileName));
writer.write("Name: " + name);
writer.newLine();
writer.write("Age: " + age);
writer.newLine();
writer.close();
System.out.println("Data written to " + fileName);

// 3. Read data from file using BufferedReader


System.out.println("\nReading data from file:");
BufferedReader reader = new BufferedReader(new
FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();

} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
scanner.close();
}
}
}

How to Run

1. Create a folder named FileIOProject.


2. Save the code above in a file named Main.java.
3. Open terminal or command prompt.
4. Compile and run:

javac Main.java
java Main

Output Example
Enter your name:
Alice
Enter your age:
23
Data written to user_data.txt

Reading data from file:


Name: Alice
Age: 23

You might also like