Java p14
Java p14
1.1 INTRODUCTION
In Java, handling input and output operations is crucial, and the IO library facilitates these tasks
efficiently. The provided examples demonstrate reading and writing both from the console and
a file. The `ConsoleIOExample` utilizes `BufferedReader` to read user input from the console,
displaying the entered content, and then prompting the user to write to the console. Meanwhile,
file and `BufferedReader` to read its content. These fundamental Java IO operations are
automatically closing the streams. Understanding these basic IO principles is fundamental for
a) BufferedReader:
Purpose: Used for efficient reading of characters from a character-based input stream, such
as a FileReader or an InputStreamReader.
Example Use: BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
b) InputStreamReader:
Purpose: Reads bytes and decodes them into characters. It is often used to wrap System.in
to convert bytes from the standard input stream into characters.
Example Use: new InputStreamReader(System.in)
c) IOException:
Purpose: Represents an exception thrown when dealing with input/output operations that
may result in an error or unexpected situation.
Example Use: Exception handling is crucial when working with IO operations: catch
(IOException e) { e.printStackTrace(); }
d) FileReader:
Purpose: Convenience class for reading character files. It wraps FileInputStream, providing
a FileReader that reads bytes from a file and converts them into characters using the default
character encoding.
Example Use: new FileReader(“example.txt”)
e) BufferedWriter:
Purpose: Used for efficient writing of characters to a character-based output stream, such
as a FileWriter.
Example Use: BufferedWriter writer = new BufferedWriter (new
FileWriter(“example.txt”));
f) FileWriter:
Purpose: Convenience class for writing character files. It wraps FileOutputStream,
providing a FileWriter that writes bytes to a file and converts them into characters using
the default character encoding.
Example Use: new FileWriter(“example.txt”)
These classes are part of the java.io package and are commonly used for handling input and
output operations in Java applications. They provide abstractions and utilities to work with
files, streams, and readers/writers efficiently.
//Java program to read from console and write on console using IO stream.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadWriteconsole {
public static void main(String[] args) {
// Reading from console
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)))
{
System.out.println("Enter something:");
String userInput = reader.readLine();
System.out.println("You entered: " + userInput);
// Writing to console
System.out.println("Enter something to write to console:");
String output = reader.readLine();
System.out.println("You wrote to console: " + output);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*Output*/