0% found this document useful (0 votes)
3 views12 pages

Java IO Programming

The document provides an overview of Java I/O (Input/Output), detailing the mechanisms for reading and writing data using various packages like java.io and java.nio. It explains different types of I/O, including console, file, and stream I/O, and highlights important classes such as FileInputStream, FileOutputStream, FileReader, and FileWriter. Additionally, it covers exception handling in I/O operations and presents a summary table of key classes used for reading, writing, and input/output operations.

Uploaded by

2024ht66538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Java IO Programming

The document provides an overview of Java I/O (Input/Output), detailing the mechanisms for reading and writing data using various packages like java.io and java.nio. It explains different types of I/O, including console, file, and stream I/O, and highlights important classes such as FileInputStream, FileOutputStream, FileReader, and FileWriter. Additionally, it covers exception handling in I/O operations and presents a summary table of key classes used for reading, writing, and input/output operations.

Uploaded by

2024ht66538
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java I/O (Input/Output)

Programming
• An overview of streams, console and file
operations in Java
What is I/O in Java?
• - I/O = Input / Output
• - Mechanism to read/write data
• - Packages: java.io, java.nio
Types of I/O in Java
• Console I/O: Keyboard and screen
• File I/O: File operations
• Stream I/O: Binary or text data
Streams in Java
• - A stream is a sequence of data
• - Byte Streams: For binary data
• - Character Streams: For text
Byte Streams
• FileInputStream: Read bytes
• FileOutputStream: Write bytes
• Example:
• FileOutputStream out = new
FileOutputStream("output.txt");
• out.write("Hello".getBytes());
• out.close();
Character Streams
• FileReader: Read characters
• FileWriter: Write characters
• Example:
• FileWriter fw = new FileWriter("msg.txt");
• fw.write("Welcome!");
• fw.close();
Console Input/Output
• Input:
• Scanner sc = new Scanner(System.in);
• String name = sc.nextLine();
• Output:
• System.out.println("Hello");
File Reading Example
• BufferedReader reader = new
BufferedReader(new FileReader("file.txt"));
• while ((line = reader.readLine()) != null) {
• System.out.println(line);
• }
Important I/O Classes
• Scanner, PrintWriter
• BufferedReader, BufferedWriter
• ObjectInputStream, ObjectOutputStream
Exception Handling
• Use try-catch:
• try {
• // I/O code
• } catch (IOException e) {
• System.out.println(e);
• }
Summary Table
• Read: FileReader, BufferedReader
• Write: FileWriter, PrintWriter
• Input: Scanner
• Output: System.out
• Byte: InputStream, OutputStream
Thank You

You might also like