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

Java p14

This document discusses input/output (IO) operations in Java using the IO library. It provides examples of reading from and writing to both the console and files. The examples demonstrate using BufferedReader to read from the console and files, BufferedWriter to write to files, and try-with-resources blocks to automatically close streams.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java p14

This document discusses input/output (IO) operations in Java using the IO library. It provides examples of reading from and writing to both the console and files. The examples demonstrate using BufferedReader to read from the console and files, BufferedWriter to write to files, and try-with-resources blocks to automatically close streams.

Uploaded by

Khan.ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No.

14 : Write a program in Java to read and write from console


using IO library. Also write a program in Java to read and write from a file
using IO library.

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,

the `FileIOExample` showcases file operations using `BufferedWriter` to write a message to a

file and `BufferedReader` to read its content. These fundamental Java IO operations are

encapsulated within try-with-resources blocks to ensure proper resource management by

automatically closing the streams. Understanding these basic IO principles is fundamental for

building robust and interactive Java applications.

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.

1.2 Reading and Writing from Console:

//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*/

1.3 Reading and Writing from File:


//Java program to perform Reading and writing operations from and in file.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadWriteFile{


public static void main(String[] args) {
// Writing to a file
try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) {
writer.write("Hello, this is written to a file!");
} catch (IOException e) {
e.printStackTrace();
}

// Reading from a file


try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line = reader.readLine();
System.out.println("Content read from file: " + line);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*Output*/

You might also like