You are on page 1of 6

import java.io.

FileInputStream;

public class FileInputStreamDemo

public static void main (String[]args)

try

FileInputStream fis = new FileInputStream ("input.txt");

System.out.println ("Data in the file: ");

// Reads the first byte

int ch = fis.read ();

while (ch != -1)

System.out.print ((char) ch);

// Reads next byte from the file

ch = fis.read ();

fis.close ();

catch (Exception e)

e.getStackTrace ();

}
PrintWriter Class in Java

Java PrintWriter class is the implementation of the Writer class. It is used to print the formatted
representation of objects to the text-output stream. Unlike other writers, PrintWriter converts the
primitive data (int, float, char, etc.) into the text format. It then writes that formatted data to the writer.
Also, the PrintWriter class does not throw any input/output exceptions. Instead, we need to use the
checkError() method to find any error in it.

import java.io.*;

public class PrintWriterDemo

public static void main(String args[]) throws Exception

String data = "This is a line of text inside the file.";

try {

PrintWriter output = new PrintWriter("output.txt");

output.print(data);

output.close();

System.out.println("Success?");

} catch (Exception e) {

e.getStackTrace();

InputStreamReader Class in Java


The InputStreamReader class of the java.io package can be used to convert data in bytes into data in characters. It extends the abstract
class Reader. An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters
using a specified charset. The charset that it uses may be specified by name or maybe given explicitly, or the platform’s default charset may
be accepted.
OutputStreamWriter class in java
The OutputStreamWriter class of the java.io package can be used to convert data in character form into data in bytes form. It extends the
abstract class Writer. The OutputStreamWriter class works with other output streams. It is also known as a bridge between byte streams and
character streams. This is because the OutputStreamWriter converts its characters into bytes.

FileReader Class in Java


Java FileReader class is used to read data from the file. This is a character stream class that is used to read the file information char by char.
It extends the InputStreamReader class.

import java.io.*;

public class FRDemo

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

FileReader fr=new FileReader("file.txt");

int i;

while((i=fr.read())!=-1)

System.out.print((char)i);

fr.close();

FileWriter class in Java


This character stream class is used to write the content onto the file char by char. It is used for file handling in java. It extends the
OutputStreamWriter class.

import java.io.*;

public class FWDemo

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

FileWriter fw = new FileWriter ("output.txt");

String str = "This time never come back";


fw.write (str);

char chars[] = str.toCharArray ();

fw.write (chars);

for (int i = 0; i < str.length (); i++)

fw.write (str.charAt (i));

fw.flush ();

fw.close ();

System.out.println ("Success");

BufferedReader class in Java


This is a character stream class which is used to perform reading operation from any input device like keyboard, file, etc. It makes the
performance fast. It inherits the Reader class.

During the read operation in BufferedReader, a chunk of characters is read from the disk and stored in
the internal buffer. And from the internal buffer characters are read individually. Hence, the number of
communication to the disk is reduced. This is why reading characters is faster using BufferedReader.

Creating BufferedReader Object: BufferedReader buffer = new BufferedReader (Reader obj);

Example: Program to understand BufferedReader Class

import java.io.*;

public class BRDemo

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


InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);

System.out.println("Enter your name");

String name = br.readLine();

System.out.println("Welcome " + name);

Output:

Enter your name

Anurag

Welcome Anurag

BufferedWriter Class in Java

The BufferedWriter class of the java.io package can be used with other writers to write data (in
characters) more efficiently. It extends the abstract class Writer. It makes the performance fast. The
buffering characters are used for providing the efficient writing of single arrays, characters, and strings.

Creating BufferedWriter Object: BufferedWriter buffer = new BufferedWriter (file);

Program to understand BufferedWriter Class in Java

import java.io.*;

public class BWDemo

public static void main(String[] args) throws Exception

FileWriter writer = new FileWriter("file.txt");

BufferedWriter buffer = new BufferedWriter(writer);


buffer.write("Welcome to Java Programming Language.");

buffer.close();

System.out.println("Success");

You might also like