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

Unti5 Partially

Document

Uploaded by

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

Unti5 Partially

Document

Uploaded by

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

Reading console Input & Writing Console output:

Console refers to Command-Line interface or text-based interface where user enters commands
to performs various tasks
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:
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. The BufferedReader class is defined
in the java.io package.
Program on how to read console input using BufferedReader class.
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:
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. Scanner class has defined in the java.util package.
Program on how to read console input using Scanner class.
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 + "!");
}
}

1
3. Reading console input using Console class:
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.
Program:
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
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.
The print() method writes console output in the same line. The println() method writes console
output in a separete line (new line).
Program:
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 = 0; i < list.length; i++)
System.out.print(i); //prints in same line
System.out.println("");
for(int i = 0; i < list.length; i++)
System.out.println(i); //Prints in separate lines

}
}
2. Writing console output using write():
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;
2
}
for(int i = 0; i < list.length; i++) {
System.out.write(i);
System.out.write('\n');
}
}
}
Reading and Writing Files:
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)

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.

Following is the Program that reads data from a file and writes the same to another file using
FileInoutStream and FileOutputStream classes
import java.io.*;
public class FileReadingTest {
public static void main(String args[]) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("D:\\Languages.txt");
out = new FileOutputStream("D:\\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();
}
}
}
}

3
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.
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("D:\\Input-File.txt");
out = new FileWriter("D:\\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