Java FileStream

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Working with Files in JAVA

Introduction to I/O Streams in Java


 Streams are called a sequence of data. It is neither a data structure nor it
stores data. Take an example of a river stream, where water flows from source
to destination. Similarly, these are data streams; data flows from one point to
another.
 To handle these sequences, JAVA introduces a term called IO streams.
 Java I/O is the package that helps the user perform all input-output
operations. Java I/O package is primarily focused on input-output files,
network streams, internal memory buffers, etc. Data is read and written from
Java IO's InputStream and OutputStream classes.
 In other words, IO streams in java help to read the data from the file and write
the data into the file. It represents the source as input and the destination as
output. It can handle all types of data, from primitive values to advanced
objects.
Java I/O package
 The Java I/O package that is java.io consists of input and
output streams used to read and write data to files or
other input and output sources.
 There are 3 categories of classes in java.io are as follows:
• Input Streams.
• Output Streams.
• Error Streams.
Input Streams

 As we know input source consists of data that needs


to be read in order to extract information from it.
Input Streams help us to read data from the input
source. It is an abstract class that provides a
programming interface for all input streams.
 Input streams are opened implicitly as soon as it is
created. To close the input stream, we use
a close() function.
Output Streams
 The output of the executed program has to be
stored in a file for further use. Output streams
help us to write data to a output source(may be
file). Similarly like input streams output
streams are also abstract classes that provides
a programming interface for all output streams.
 The output stream is opened as soon as it is
created and explicitly closed by using
the close() method.
Error Streams

 Errorstreams are the same as output streams.


In some ide’s error is displayed in different
colors (other than the color of output color). It
gives output on the console the same as output
streams.
Need of IO Streams in Java

 In day-to-day work, we do not enter the input into the


programs manually. Also, the result of the program needs
to be stored somewhere for further use.
 So, IO streams in Java provide us with input and output
streams that help us to extract data from the files and write
the data into the files. Normally, we can create, delete, and
edit files using Java.io.
 In short, all the file manipulation is done using Java IO
streams.
Types of Streams in Java

 Depending on the types of operations, streams


are divided into 2 primary classes.
 Input Stream
 Output Stream
Input Stream
 It is an abstract superclass of the java.io package and is used
to read the data from the source. In other words, reading the
data from the files. We can create an object of the input
stream class using a new keyword. The input stream class
has several types of constructors.
 The following code takes the file name as a string, to read
the data stored in the file.
InputStream f = new FileInputStream("input.txt");
InputStream Hierarchy
Methods of InputStream
 1. public abstract int read() throws IOException
The above code helps to return the data of the next byte in
the input stream. The value returned is between 0 to 255. If
no byte is read, the code returns -1, which indicates the end
of the file.

 2. public int available() throws IOException


The above code returns the number of bytes that can be read
from the input stream.
 3. public void close() throws IOException
The above code closes the current input stream and releases
any system resources associated with it.
 4. public void mark(int readlimit)
The above code marks the current position in the input
stream. The readlimit argument tells the input stream to read
that many bytes to read before the mark position gets invalid.
 5. public boolean markSupported()
The above code tells whether the mark and reset method is
supported in a particular input stream. It returns true if the
mark and reset methods are supported by the particular
input stream or else return false.
 6. public int read(byte[ ] b) throws IOException
The above code reads the bytes from the input stream and
stores every byte in the buffer array. It returns the total
number of bytes stored in the buffer array. If there is no byte
in the input stream, it returns -1 as the stream is at the end of
the file.
 7. public int read(byte[ ] b , int off , len) throws
IOException
The above code reads up to len bytes of data from the input
stream. It returns the total number of bytes stored in the
buffer. Here the “off” is start offset in buffer array b where the
data is written, and the “len” represents the maximum
 8. public void reset() throws IOException
The above code repositions the stream to the last called
mark position. The reset method does nothing for input
stream class except throwing an exception.

 9. public long skip(long n) throws IOException


The above code discards n bytes of data from the input
stream.
FileInputStream class
import java.io.*;
class mainFile
{
public static void main(String[] args) throws IOException
{
try
{
FileInputStream f = new FileInputStream("input.txt");
int x = 0;

while ((x = f.read())!=-1)


{
System.out.print((char)x);
}
f.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 BufferedInputStream class to read the file.
import java.io.*;
class main
{
public static void main(String[] args) throws IOException
{
try
{

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


BufferedInputStream f2 = new BufferedInputStream(f1);

System.out.println("Available bytes: "+f2.available());


int x = 0;
while ((x = f2.read())!=-1)
{
System.out.print((char)x);
}
System.out.println();

f2.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

You might also like