Unit 3 - MULTITHREADING, I/O AND GENERIC
PROGRAMMING
MULTITHREADED PROGRAMMING: CREATING A THREAD, THREAD
PRIORITIES, SYNCHRONIZATION, INTERTHREAD COMMUNICATION –
I/O: I/O BASICS, READING CONSOLE INPUT, WRITING CONSOLE
OUTPUT, READING AND WRITING FILES – GENERICS:
INTRODUCTION, GENERIC CLASS, BOUNDED TYPES, GENERIC
METHODS, GENERIC INTERFACES, GENERIC RESTRICTIONS.
Unit 3 – Input/Output
I/O
• I/O BASICS
• READING CONSOLE INPUT
• WRITING CONSOLE OUTPUT
• READING AND WRITING FILES
I/O Basics: Definition
I/O (Input/Output) refers to how a program interacts with the
outside world, allowing it to take input from users or files and display
output.
•Input: Data provided to a program (e.g., user input from a keyboard,
reading a file).
•Output: Data produced by a program (e.g., printing to the screen,
writing to a file).
•Java provides various classes in the [Link] package for handling I/O
operations.
Reading Console Input (User Input)
Definition:
Reading console input means getting user input from the keyboard
during program execution.
This is done using Scanner or BufferedReader.
Syntax:
Scanner scanner = new Scanner([Link]);
String input = [Link](); // Reads a line of input
Reading Console Input: Analogy
Imagine you're at a karaoke night.
The singer (your program) waits for you (user) to select a song
(input).
Once you choose a song, the singer starts performing!
Similarly, Java programs wait for user input and proceed accordingly.
Reading Console Input: Example
import [Link];
Output:
public class Example { Enter your name: Alice
Hello, Alice!
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link](); // Reading input
[Link]("Hello, " + name + "!"); // Displaying output
[Link]();
}}
Writing Console Output
Definition:
Writing console output means displaying information on the
screen using [Link]() or [Link]().
Syntax:
[Link]("Hello, World!");
Writing Console Output - Analogy
Imagine a news anchor reading out the latest news on live
TV.
The anchor (Java program) delivers messages to the
audience (user) via the screen (console).
Writing Console Output - Example
public class ConsoleOutputExample {
public static void main(String[] args) {
[Link]("Java is fun!"); // Print output
}}
Output:
Java is fun!
Reading and Writing Files
Definition:
Reading and writing files involve accessing data stored in files on disk using
FileReader, BufferedReader (for reading), FileWriter, and BufferedWriter (for
writing).
Syntax: Reading a file:
BufferedReader reader = new BufferedReader(new FileReader("[Link]"));
String line = [Link]();
Writing to a file:
BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"));
[Link]("Hello, Java!");
Reading and Writing Files - Analogy
Think of a diary.
When you read a diary, you are performing file reading.
When you write your thoughts into it, you are performing file writing.
Reading and Writing Files – Example - 1
import [Link].*;
public class FileIOExample {
public static void main(String[] args) {
try {
// Writing to a file
BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"));
[Link]("Hello, this is a file example!");
[Link]();
Reading and Writing Files – Example - 1
// Reading from a file
BufferedReader reader = new BufferedReader(new FileReader("[Link]"));
String line = [Link]();
[Link]("File Content: " + line);
[Link]();
}
Output:
catch (IOException e) { File Content: Hello, this is a file
[Link](); example!
}
}}
Reading and Writing Files – Example – 2
Reading Multiple Lines from a File
import [Link].*; Assume [Link] contains:
public class ReadFileExample { Hello, Java!
Welcome to file handling.
public static void main(String[] args) { Reading multiple lines is easy.
String fileName = "[Link]"; // File to read
try {
// Open the file for reading
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
Reading and Writing Files – Example – 2
Reading Multiple Lines from a File
[Link]("Reading file content:");
while ((line = [Link]()) != null) { // Read line by line
[Link](line);
}
// Close the reader
[Link](); } Output:
Reading file content:
catch (IOException e) { Hello, Java!
[Link](); // Print error details if file not found Welcome to file handling.
Reading multiple lines is easy.
}}}