Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
50 views

Java IO

This document discusses various ways to perform console and file input/output operations in Java. There are three main ways to read console input: using BufferedReader, Scanner, or Console classes. BufferedReader wraps System.in in an InputStreamReader, Scanner also wraps System.in, and Console reads from the console if it is available. There are two main ways to write console output: using print() and println() methods of PrintStream, or the write() method. File I/O can be done with byte streams using FileInputStream and FileOutputStream, or character streams using FileReader and FileWriter.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Java IO

This document discusses various ways to perform console and file input/output operations in Java. There are three main ways to read console input: using BufferedReader, Scanner, or Console classes. BufferedReader wraps System.in in an InputStreamReader, Scanner also wraps System.in, and Console reads from the console if it is available. There are two main ways to write console output: using print() and println() methods of PrintStream, or the write() method. File I/O can be done with byte streams using FileInputStream and FileOutputStream, or character streams using FileReader and FileWriter.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Console Class:-

Console IO Operations in Java

Reading console input in java


In java, there are three ways to read console input. Using the 3 following ways, we can read input data from the console.

 Using BufferedReader class


 Using Scanner class
 Using Console class

1. Reading console input using BufferedReader class in java


Reading input data using the BufferedReader class is the traditional technique. This way of the reading method is used by wrapping
the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the
console.
The BufferedReader class has defined in the java.io package.

import java.io.*;
public class ReadingDemo {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name = "";
try {
System.out.print("Please enter your name : ");
name = in.readLine();
System.out.println("Hello, " + name + "!");
}
catch(Exception e) {
System.out.println(e);
}
finally {
in.close();
}
}
}

2. Reading console input using Scanner class in java


Reading input data using the Scanner class is the most commonly used method. This way of the reading method is used by wrapping
the System.in (standard input stream) which is wrapped in a Scanner, we can read input from the console.
The Scanner class has defined in the java.util package.

import java.util.Scanner;
public class ReadingDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = "";
System.out.print("Please enter your name : ");
name = in.next();
System.out.println("Hello, " + name + "!");

}
}

3. Reading console input using Console class in java


Reading input data using the Console class is the most commonly used method. This class was introduced in Java 1.6 version.
The Console class has defined in the java.io package.

import java.io.*;

public class ReadingDemo {


public static void main(String[] args) {
String name;
Console con = System.console();

if(con != null) {
name = con.readLine("Please enter your name : ");
System.out.println("Hello, " + name + "!!");
}
else {
System.out.println("Console not available.");
}
}
}

Writing console output in java


In java, there are two methods to write console output. Using the 2 following methods, we can write output data to the console.

 Using print() and println() methods


 Using write() method

Let's explore the each method to write data with example.

1. Writing console output using print() and println() methods


The PrintStream is a bult-in class that provides two methods print() and println() to write console output. The print() and println() methods are
the most widely used methods for console output.
Both print() and println() methods are used with System.out stream.

public class WritingDemo {


public static void main(String[] args) {
int[] list = new int[5];
for(int i = 0; i < 5; i++)
list[i] = i*10;
for(int i:list)
System.out.print(i); //prints in same line
System.out.println("");
for(int i:list)
System.out.println(i); //Prints in separate lines
}}

2. Writing console output using write() method


Alternatively, the PrintStream class provides a method write() to write console output.
The write() method take integer as argument, and writes its ASCII equalent character on to the console, it also acept escape sequences.

public class WritingDemo {


public static void main(String[] args) {
int[] list = new int[26];
for(int i = 0; i < 26; i++) {
list[i] = i + 65;
}
for(int i:list) {
System.out.write(i);
System.out.write('\n');
}
}
}

File Reading & Writing in Java


In java, there multiple ways to read data from a file and to write data to a file. The most commonly used
ways are as follows.

Using Byte Stream (FileInputStream and FileOutputStream)


Using Character Stream (FileReader and FileWriter)
Let's look each of these ways.

File Handling using Byte Stream


In java, we can use a byte stream to handle files. The byte stream has the following built-in classes to
perform various operations on a file.

FileInputStream - It is a built-in class in java that allows reading data from a file. This class has
implemented based on the byte stream. The FileInputStream class provides a method read() to read
data from a file byte by byte.
FileOutputStream - It is a built-in class in java that allows writing data to a file. This class has
implemented based on the byte stream. The FileOutputStream class provides a method write() to write
data to a file byte by byte.
Let's look at the following example program that reads data from a file and writes the same to another
file using FileInputStream and FileOutputStream classes.

Example
import java.io.*;
public class FileReadingTest {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;

try {
in = new FileInputStream("C:\\OM\\Input-File.txt");
out = new FileOutputStream("C:\\OM\\Output-File.txt");

int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("Reading and Writing has been success!!!");
}
catch(Exception e){
System.out.println(e);
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

File Handling using Character Stream


In java, we can use a character stream to handle files. The character stream has the following built-in classes to perform various operations
on a file.

 FileReader - It is a built-in class in java that allows reading data from a file. This class has implemented based on the character
stream. The FileReader class provides a method read() to read data from a file character by character.
 FileWriter - It is a built-in class in java that allows writing data to a file. This class has implemented based on the character
stream. The FileWriter class provides a method write() to write data to a file character by character.

Let's look at the following example program that reads data from a file and writes the same to another file using FIleReader and FileWriter
classes.

import java.io.*;
public class FileIO {

public static void main(String args[]) throws IOException {


FileReader in = null;
FileWriter out = null;

try {
in = new FileReader("C:\\OM\\Input-File.txt");
out = new FileWriter("C:\\OM\\Output-File.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
System.out.println("Reading and Writing in a file is done!!!");
}
catch(Exception e) {
System.out.println(e);
}
finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

You might also like