Java IO
Java IO
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();
}
}
}
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 + "!");
}
}
import java.io.*;
if(con != null) {
name = con.readLine("Please enter your name : ");
System.out.println("Hello, " + name + "!!");
}
else {
System.out.println("Console not available.");
}
}
}
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();
}
}
}
}
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 {
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();
}
}
}
}