IO Streams in Java
IO Streams in Java
Dr.K.Kartheeban
I/O
Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g.
harddisk
output - stream
Program Device
input - stream
Program Device
CIS 068
Overview of I/O Streams
To bring in information, a program opens a stream on
an information source (a file, memory, a socket) and
reads the information sequentially, as shown in the
following figure.
12/31/24 4
Overview of I/O STREAMS Contd.
Similarly, a program can send information to an
external destination by opening a stream to a
destination and writing the information out
sequentially, as shown in the following figure.
12/31/24 6
Overview of I/O streams Contd..
CIS 068
Having
1. InputStream: byte-input
2. OutputStream: byte-output
3. Reader: text-input
4. Writer: text-output
InputStream vs OutputStream
InputStream
Java application uses an input stream to read data from a source, it may be
a file, an array, peripheral device or socket.
OutputStream
Java application uses an output stream to write data to a destination, it
may be a file, an array, peripheral device or socket.
OutputStream
binary
Reader
Writer
text
CIS 068
Common Method in InputStream
Method Description
public abstract int read() throws reads the next byte of data from
IOException the input stream. It returns -1 at
the end of file.
public int available() throws returns an estimate of the number
IOException of bytes that can be read from the
current input stream.
public void close() throws IOException is used to close the current input
stream.
Common Method in OutputStream
Method Description
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e)
{
System.out.println(e);
}
} }
Note: Before running the code, a text file named as "testout.txt" is required
to be created. In this file, we are having following content:
After executing the above program, you will get a single character from the
file which is 75 (in byte form).
Output:
K
import java.io.*;
class SimpleRead
{
public static void main(String args[])
{
try
{
FileInputStream fin=new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1)
{
System.out.println((char)i);
}
fin.close();
}
catch(Exception e)
{
system.out.println(e);
}
} Output:
} Kartheeban welcomes you all for java programming .
Methods
Method Description
int available() It is used to return the estimated number of
bytes that can be read from the input stream.
int read() It is used to read the byte of data from the input
stream.
int read(byte[] b) It is used to read up to b.length bytes of data
from the input stream.
int read(byte[] b, int off, int It is used to read up to len bytes of data from
len) the input stream.
long skip(long x) It is used to skip over and discards x bytes of
data from the input stream.
FileChannel getChannel() It is used to return the unique FileChannel
object associated with the file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
protected void finalize() It is used to ensure that the close method is call
when there is no more reference to the file input
stream.
void close() It is used to closes the stream.
Reading the data of current java file and writing it into
another file
import java.io.*;
class C
{
public static void main(String args[])throws Exception
{
FileInputStream fin=new FileInputStream("C.java");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1)
{
fout.write((byte)i);
}
fin.close();
} }
Java FileOutputStream class
class StringTest
{
public static void main(String args[])
{
try
{
FileOutputstream fout=new FileOutputStream("abc.txt");
String s=" Kartheeban welcomes you all for java programming“;
void write(byte[] ary) It is used to write ary.length bytes from the byte array to
the file output stream.
void write(byte[] ary, int off, int It is used to write len bytes from the byte array starting at
len) offset off to the file output stream.
void write(int b) It is used to write the specified byte to the file output
stream.
FileChannel getChannel() It is used to return the file channel object associated with
the file output stream.
FileDescriptor getFD() It is used to return the file descriptor associated with the
stream.
• When the bytes from the stream are skipped or read, the internal buffer
automatically refilled from the contained input stream, many bytes at a
time.
while((i=bin.read())!=-1){
System.out.print((char)i);
}
bin.close();
fin.close();
}catch(Exception e){System.out.println(e);}
}
} Kartheeban welcomes you all for java programming.
Methods
Method Description
int available() It returns an estimate number of bytes that can be read from the
input stream without blocking by the next invocation method for
the input stream.
int read() It read the next byte of data from the input stream.
int read(byte[] b, int off, It read the bytes from the specified byte-input stream into a
int ln) specified byte array, starting with the given offset.
void close() It closes the input stream and releases any of the system
resources associated with the stream.
void reset() It repositions the stream at a position the mark method was last
called on this input stream.
void mark(int readlimit) It sees the general contract of the mark method for the input
stream.
long skip(long x) It skips over and discards x bytes of data from the input stream.
boolean It tests for the input stream to support the mark and reset
markSupported() methods.
Java BufferedOutputStream Class
Java BufferedOutputStream class is used for buffering an output stream.
It adds more efficiency than to write data directly into a stream. So, it makes
the performance fast.
byte b[]=s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
}
}
Java ByteArrayInputStream Class
The ByteArrayInputStream is composed of two words: ByteArray and
InputStream.
As the name suggests, it can be used to read byte array as input stream.
Constructor Description
In this stream, the data is written into a byte array which can be
written to multiple streams later.
Java ByteArrayOutputStream class
constructors
Constructor Description
bout.writeTo(fout1);
bout.writeTo(fout2);
bout.flush();
bout.close();//has no effect
System.out.println("Success...");
}
}
Methods
Method Description
int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
}
}}
Java FileWriter class
Java FileWriter class is used to write character-oriented data to a
file.
Method Description
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
}
import java.io.*;
class Input1
{
public static void main(String args[]) throws Exception
{
FileInputStream fin1=new FileInputStream("D:\\testin1.txt");
FileInputStream fin2=new FileInputStream("D:\\testin2.txt");
int i;
while((i=sis.read())!=-1)
{
fout.write(i);
}
sis.close();
fout.close();
fin1.close();
fin2.close();
System.out.println("Success..");
}
}