You are on page 1of 9

Input/Output in Java

Using Character Stream


Using BufferedReader class reading from standard input.
By wrapping the System.in (standard input stream) in
an InputStreamReader which is wrapped in a BufferedReader,
we can read input from the user in the command line. Here’s an
example:
BufferedReader reader = new BufferedReader(new
1
InputStreamReader(System.in));
2
System.out.print("Enter your name: ");
3
4 String name = reader.readLine();
5 System.out.println("Your name is: " + name);
In the above example, the readLine() method reads a line of text
from the command line.

//program reading a single character

import java.io.*;

public class ReadConsole {

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

InputStreamReader cin = null;

try {

cin = new InputStreamReader(System.in);


System.out.println("Enter characters, 'q' to quit.");

char c;

do {

c = (char) cin.read();

System.out.print(c);

} while(c != 'q');

}finally {

if (cin != null) {

cin.close();

Using BufferedWriter class writing to standard output.

try { BufferedWriter log = new


BufferedWriter(new OutputStreamWriter(System.out));

log.write("This will be printed on stdout!\n");


log.flush();
}
catch (Exception e) {
e.printStackTrace();
}
Reading from a file using FileReader and BufferedReader.

import java.io.*;
public class Test {
public static void main(String [] args) {

// The name of the file to open.


String fileName = "temp.txt";

// This will reference one line at a time


String line = null;

try {
// FileReader reads text files in the def
ault encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedRead


er.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine())
!= null) {
System.out.println(line);
}

// Always close files.


bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}

Writing to a file FileWriter and BufferedWriter


import java.io.*;

public class Test {


public static void main(String [] args) {

// The name of the file to open.


String fileName = "temp.txt";

try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName);
// Always wrap FileWriter in
BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);

// Note that write() does not


automatically
// append a newline character.
bufferedWriter.write("Hello there,");
bufferedWriter.write(" here is some
text.");
bufferedWriter.newLine();
bufferedWriter.write("We are writing");
bufferedWriter.write(" the text to the
file.");

// Always close files.


bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Using Byte stream
Reading and Writing a file in Java using ‘Byte Streams’
Java provides a number of byte streams classes, all these classes are decedents from either
‘InputStream’ or ‘OutputStream’. In the example below we have used ‘FileInputStream’ and
‘FileOutputStream’ class.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteStreamDemo {

public static void main(String args[]) throws


IOException {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;

try {
int data;
fileInputStream = new
FileInputStream("files/source.txt");
fileOutputStream = new
FileOutputStream("files/destination.txt");
while ((data = fileInputStream.read()) != -1) {
System.out.print(" " + data);
fileOutputStream.write(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null)
fileInputStream.close();
if (fileOutputStream != null)
fileOutputStream.close();
}
}
}

read() and write() functions are used here, read() returns a integer value equals to next byte in
the file and -1 if no more byte is left to read. The write() methods accepts a byte as parameter to
write.

Reading from standard input using stream classes


BufferedInputStream bf= new BufferedInputStream(System.in);
System.out.println("Enter characters, 'q' to quit.");
char c;
try{
do {
c = (char) bf.read();
System.out.print(c);
} while(c != 'q');
}catch(IOException e){System.out.println(e);}

Write to standard output using stream classes


BufferedInputStream bf= new BufferedInputStream(System.in);
BufferedOutputStream bf1= new
BufferedOutputStream(System.out);
System.out.println("Enter characters, 'q' to quit.");
char c;
try{
do {
c = (char) bf.read();
//System.out.print(c);
bf1.write(c);bf1.flush();
} while(c != 'q');
}catch(IOException e){System.out.println(e);}

You might also like