You are on page 1of 29

 

 
File  
Stream
A stream is a sequence of bytes that has a source(input
stream ) or a destination(output stream).

A flow of data is often refered to as a data stream.

Standard Stream:

In java, 3 streams are created for us automatically. All these


streams are attached with console.

1) System.out: standard output stream


2) System.in: standard input stream
3) System.err: standard error stream
The Byte Stream Classes
Byte Streams, which are used for handling I/O operation in
bytes.

Byte streams are defined by using two class hierarchies.

At the top are two abstract classes: InputStream and


OutputStream.
InputStream:

Java.io.InputStream is an abstract class that contains the


basic methods for reading raw bytes of data from a stream.
Methods Descriptions
It reads byte of data from the
1)read() input stream. It returns -1 at the
end of file.
It reads array of bytes from
2)read(byte arr[])
input stream.
It reads n bytes of array ,start
3)read(byte arr[],int s,int n)
from s position
returns available number of
4)available()
bytes.
Moves to beginning of the
5)reset()
stream
6)close() Input stream is close.
OutputStream class
Java.io.OutputStream is an abstract class that contains
the basic methods for writing raw bytes of data from a
stream.

Methods Description

1) public void write(int)throws is used to write a byte to the


IOException current output stream.

is used to write an array of


2) public void write(byte[])throws
byte to the current output
IOException
stream.

3) public void flush()throws flushes the current output


IOException stream.

4) public void close()throws is used to close the current


IOException output stream.
Byte Stream Classes Meaning

DataInputStream An input stream that contains methods


for reading the Java standard data
types
DataOutputStream An output stream that contains
methods for writing the Java standard
data types
BufferedInputStream Buffered input stream

BufferedOutputStream Buffered output stream

ByteArrayInputStream Input stream that reads from a byte


array
ByteArrayOutputStream Output stream that writes to a byte
array
ByteStream Classes Meaning

FileInputStream Input stream that reads from a file

FileOutputStream Output stream that writes to a file

PrintStream Output stream that contains print( ) and


println( )
FileInputStream

The FileInputStream class creates an InputStream that


you can use to read bytes from a file.

Constructors are shown here:


FileInputStream(String filepath)
FileInputStream(File fileObj)

FileInputStream f0 = new FileInputStream(“abc.txt")


File f = new File(“xyz.txt");
FileInputStream f1 = new FileInputStream(f);
// Demonstration of File input stream.
import java.io.*;
class file_input_stream
{
public static void main(String args[])
{
try
{
int i,s;
File f=new File("abc.txt");
FileInputStream fin=new FileInputStream(f);
s=fin.available(); System.out.println("Total Available size
is: "+s);
do
{
i = fin.read();
if(i != -1)
System.out.print((char) i);
} while(i != -1);
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
FileOutputStream

FileOutputStream creates an OutputStream that you can


use to write bytes to a file.

constructors are shown here:

FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
// Demonstration of File input stream.
import java.io.*;
class file_output_stream
{
public static void main(String args[])
{
try
{
int i,s;
FileInputStream fin=new FileInputStream("abc.txt");
FileOutputStream fout=new FileOutputStream("a.txt");
try
{
do
{
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
System.out.println("Successfully copied file");
}
catch(IOException e)
{
System.out.println("File Error");
}
}
}
The Character Stream Classes
Character streams are defined by using two class hierarchies. At
the top are two abstract classes, Reader and Writer.

Character Stream Meaning


Classes
BufferedReader Buffered input character stream
BufferedWriter Buffered output character stream
InputStreamReader Input stream that translates bytes
to characters
OutputStreamWriter Output stream that translates
characters
to bytes
FileReader Input stream that reads from a
file
Filewriter Output stream that writes to a
file
Character Stream Meaning
Classes
StringReader Input stream that reads from a
string
StringWriter Output stream that writes to a string

CharArrayReader Input stream that reads from a


character
CharArrayWriter Output stream that writes to a
character array
PrintWriter Output stream that contains print( )
and
println( )
FileReader
The FileReader class creates a Reader that you can use to
read the contents of a file. Its two most commonly used
constructors are shown here:

FileReader(String filePath)
FileReader(File fileObj)
// Demonstrate FileReader.
import java.io.*;
class filereaderdemo
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("filereaderdemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
{
System.out.println(s);
}
fr.close();
}
}
FileWriter

FileWriter creates a Writer that you can use to write


character to a file.

constructors are shown here:


FileWriter(String filePath)
FileWriter(String filePath, boolean append)
FileWriter(File fileObj)
FileWriter(File fileObj, boolean append)
import java.io.*;
class filewriterdemo
{
public static void main(String args[]) throws Exception
{
String source = "Hello Friends! How r u?";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);

FileWriter fw = new FileWriter("a.txt",true);


for (int i=0; i < buffer.length; i ++)
{
fw.write(buffer[i]);
}
fw.close();

FileWriter f1 = new FileWriter("b.txt");


f1.write(buffer);
f1.close();

}
File Class:

File class does not specify how information is retrieved from or


stored in files; it describes the properties of a file itself.

A File object is used to obtain or manipulate the information


associated with a disk file, such as the permissions, time, date,
and directory path, and to navigate subdirectory hierarchies.

The following constructors can be used to create File objects:s


File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File(URI uriObj)
InputStreamReader

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 may be


given explicitly, or the platform's default charset may be
accepted.
Constructor name Description

InputStreamReader(InputStream It creates an InputStreamReader


in) that uses the default charset.

InputStreamReader(InputStream It creates an InputStreamReader


in, Charset cs) that uses the given charset.

It creates an InputStreamReader
InputStreamReader(InputStream
that uses the given charset
in, CharsetDecoder dec)
decoder.

InputStreamReader(InputStream It creates an InputStreamReader


in, String charsetName) that uses the named charset.
Modifier and
Method Description
Type

It closes the stream and


void close() releases any system resources
associated with it.

It returns the name of the


String getEncoding() character encoding being used
by this stream.

int read() It reads a single character.

It reads characters into a


read(char[] cbuf, int
int portion of an array
offset, int length)
.

It tells whether this stream is


boolean ready()
ready to be read.
OutputStreamWriter
OutputStreamWriter is a class which is used to convert
character stream to byte stream, the characters are encoded
into byte using a specified charset.

write() method calls the encoding converter which converts the


character into bytes.

The resulting bytes are then accumulated in a buffer before


being written into the underlying output stream. The characters
passed to write() methods are not buffered.
Constructor Description

It creates an OutputStreamWriter
OutputStreamWriter(OutputStream
that uses the default character
out)
encoding.

OutputStreamWriter(OutputStream It creates an OutputStreamWriter


out, Charset cs) that uses the given charset.

OutputStreamWriter(OutputStream It creates an OutputStreamWriter


out, CharsetEncoder enc) that uses the given charset encoder.

OutputStreamWriter(OutputStream It creates an OutputStreamWriter


out, String charsetName) that uses the named charset.
Modifier and Type Method Description
It closes the stream,
void close()
flushing it first.

void flush() It flushes the stream.

It returns the name of


the character encoding
String getEncoding()
being used by this
stream.

write(char[] cbuf, int off, It writes a portion of an


void
int len) array of characters.

It writes a single
void write(int c)
character.
write(String str, int off, It writes a portion of a
void
int len) string.
BufferedReader Class

Java BufferedReader class is used to read the text


from a character-based input stream.

It can be used to read data line by line by readLine()


method. It makes the performance fast. It inherits
Reader class.
It is used to create a buffered
BufferedReader(Reader rd) character input stream that uses the
default size for an input buffer.

It is used to create a buffered


BufferedReader(Reader rd, int
character input stream that uses the
size)
specified size for an input buffer.
Method Description
int read() It is used for reading a single character.
int read(char[] cbuf, int off, It is used for reading characters into a
int len) portion of an array.
It is used to test the input stream support
boolean markSupported()
for the mark and reset method.
String readLine() It is used for reading a line of text.
It is used to test whether the input stream is
boolean ready()
ready to be read.
long skip(long n) It is used for skipping the characters.
It repositions the stream at a position the
void reset() mark method was last called on this input
stream.
void mark(int It is used for marking the present position in
readAheadLimit) a stream.
It closes the input stream and releases any
void close() of the system resources associated with the
stream.

You might also like