Streams(IO Streams) in Java

Posted on

Character arraysReader class:
   CharArrayReader is an implementation of an InputStream that uses a character array as the source. This class has two constructors.

  1. CharArrayReader(Char array[]);
  2. CharArrayReader(char array[], int start, int numchar);
import java.io.*;
class App90
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
File f=new File(args[0]);
char x[]=new char[(int)f.length()];
reader.read(x);
reader.close();
CharArrayReader car=new CharArrayReader(x);
int n;
n=car.read();
while(n!=-1)
{
System.out.print((char)n);
n=car.read();
}
car.close();
}
}


output:

\>javac App90.java
\>java App90 sample.txt
Hello World

\>type sample.txt
Hello World

CharArrayWriter class:
   CharArrayWriter class is an implementation of an output stream that uses an array as the destination.

Constructor:
CharArrayWriter();CharArrayWriter(int numchar); The buffer is held in the fields “buf”. The buffer size is automatically increased if necessary.
Methods:
Char[] toCharArray(); writes the contents of the buffer into a character array.Void writeTo(Filewriter object); Allows you to write the contents of a file.

import java.io.*;
class App91
{
public static void main(String args[]) throws IOException
{
String s=new String("Hello World");
char x[]=s.toCharArray();
CharArrayWriter caw=new CharArrayWriter();
caw.write(x);
char p[]=caw.toCharArray();
System.out.println("Printing the contents of an array ...");
for(int i=0;i System.out.print(p[i]);
Writer writer=new FileWriter(args[0]);
caw.writeTo(writer);
caw.close();
writer.close();
}
}


output:

\>javac App91.java
\>java App91 sample.txt
Printing the contents of an array ...
Hello World

\>type sample.txt
Hello World

Buffered Reader class:
   Buffered Reader improves performance by buffering input.

Constructor:

  1. BufferedReader(Reader inputstream);
  2. BufferedReader(Reader inputstream, int bufsize);

Method:

  1. String readLine(); Read line of text.

InputStream Read class:
   Allows you to create a reader for the standard input device.

Constructor:

  1. InputStreamReader(Inputstream object);

    Example:

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
                                 (or)
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


PrintWriter class:
   PrintWriter allows you to flush contents into the standard output device.

Constructor:
PrintWriter(OutputStream outputStream);PrintWriter(OutputStream outputStream, boolean flushonnewline);PrintWriter(Writer outputstream);PrintWriter(Writer outputStream, boolean flushonnewline);

import java.io.*;
class App92
{
public static void main(String args[]) throws IOException
{
BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out),true);
int x,y,sum;
pw.println("Enter the first number :");
x=Integer.parseInt(cin.readLine());
pw.println("Enter the second number :");
y=Integer.parseInt(cin.readLine());
sum=x+y;
pw.println("Sum of numbers :"+sum);
}
}


output:

\>javac App92.java
\>java App92
Enter the first number :
10
Enter the second number :
20
Sum of numbers :30