Streams(IO Streams) in Java

Posted on

Character Streams :         The “Character” streams allow you to read and write information purely in the form of characters. They are ruled by two topmost classes namely

  1. Reader Class.
  2. Writer Class.

which are again abstract classes.

Reader Class :
   An Abstract class all of whose methods generate an IOException, which must be caught.

Methods :
void close(); Closes the stream.int read(); Returns the integer representation of the next available character for input. Returns -1 at the End of the File.void read(char buffer[ ]); Attempts to read upto buffer.length characters from the invoking stream into the character array “buffer”.void read(char buffer[ ], int offset, int numChars); Attempts to read “numChars” characters starting at “offset” into the character array “buffer”.
Writer Class :
   An abstract class all of whose methods would raise an IOException.

Methods :
void close(); Closes the invoking stream.void write(int n); Writes the integer representation of the next available character for output.void write(char buffer[ ]); Attempts to write the contents of the character array “buffer” into the invoking stream.void write(char buffer[ ], int offset, int numChars); Attempts to write “numChars” characters starting at “offset” from the character array “buffer” into the invoking stream.void write(String str); Attempts to write the contents of the ‘String’ object into the invoking stream.
FileReader Class :

  1. FileReader(String filePath);
  2. FileReader(File fileObject);
import java.io.*;
class App83
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
int n;
n=reader.read();
while(n!=-1)
{
System.out.print((char)n);
n=reader.read();
}
reader.close();
}
}


output:

\>javac App83.java
\>java App83 App83.java
class App83
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
int n;
n=reader.read();
while(n!=-1)
{
System.out.print((char)n);
n=reader.read();
}
reader.close();
}
}
import java.io.*;
class App84
{
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();
System.out.println("Contents of the array ...");
for(int i=0;i System.out.print(x[i]);
}
}


output:

\>javac App84.java
\>java App84 App84.java
Contents of the array ...
import java.io.*;
class App84
{
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();
System.out.println("Contents of the array ...");
for(int i=0;i System.out.print(x[i]);
}
}
import java.io.*;
class App85
{
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,0,200);
reader.close();
System.out.println("Contents of the array ...");
for(int i=0;i<200;i++)
System.out.print(x[i]);
}
}


output:

\>javac App85.java
\>java App85 App85.java
Contents of the array ...
import java.io.*;
class App85
{
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.len

File Writer class:

import java.io.*;
class App86
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
Writer writer=new FileWriter(args[1]);
int n;
n=reader.read();
while(n!=-1)
{
writer.write(n);
n=reader.read();
}
reader.close();
writer.close();
}
}


output:

\>javac App86.java
\>java App86 App86.java sample.txt
\>type sample.txt
import java.io.*;
class App86
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
Writer writer=new FileWriter(args[1]);
int n;
n=reader.read();
while(n!=-1)
{
writer.write(n);
n=reader.read();
}
reader.close();
writer.close();
}
}
import java.io.*;
class App87
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
File f=new File(args[0]);
Writer writer=new FileWriter(args[1]);
char x[]=new char[(int)f.length()];
reader.read(x);
writer.write(x);
reader.close();
writer.close();
}
}


output:

\>javac App87.java
\>java App87 App87.java sample.txt
\>type sample.txt
import java.io.*;
class App87
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
File f=new File(args[0]);
Writer writer=new FileWriter(args[1]);
char x[]=new char[(int)f.length()];
reader.read(x);
writer.write(x);
reader.close();
writer.close();
}
}

import java.io.*;
class App88
{
public static void main(String args[]) throws IOException
{
Reader reader=new FileReader(args[0]);
File f=new File(args[0]);
Writer writer=new FileWriter(args[1]);
char x[]=new char[(int)f.length()];
reader.read(x);
writer.write(x,0,100);
reader.close();
writer.close();
}
}


output:

\>javac App88.java
\>java App88 App88.java sample.txt
\>type sample.txt
import java.io.*;
class App88
{
public static void main(String args[]) throws IOException
{

import java.io.*;
class App89
{
public static void main(String args[]) throws IOException
{
String s=new String("Hello World");
Writer writer=new FileWriter(args[0]);
writer.write(s);
writer.close();
}
}


output:

\>javac App89.java
\>java App89 sample.txt
\>type sample.txt
Hello World