[edit] Java InputStream Class
|
| The streaming byte Input of Java is defined by this Abstract Class .all the methods in this class will always throw an IOException whenever any kind of error will occur.
Few methods of this class are as under:
1. available( ): This method will return the number of bytes of input which is currently available for reading.
2. close( ) : this method is used to close the input source. Once the input is closed if we will try to read it again we will get the IOException.
3. mark(int numBytes): This method places a mark at the current point in the input stream which will remain valid until all the numBytes bytes are read.
4. markSupported( ) : This method since as Boolean will return “true “ if mark( )/reset( ) are supported by the invoking stream.
5. read( ) : This method returns an integer representation of the next available byte of input. –1 is returned when the end of the file is encountered.
6. reset( ) : This method will reset the input pointer to the previously set mark.
|
[edit] Java OutputStream Class
|
|
This abstract class defines the streaming byte output. All of the methods in this class return a void value and throw an IOException in the case of errors..
Few methods of this class are as under:
1. close( ) : This method will close Closes the output stream. After the output is closed it will generate an IOException if we will try to perform any operation on it.
2. flush( ) : This method finalizes the output state so that any buffers are cleared. It means that it flushes the output buffers.
3. write(int b) : This simple method writes a single byte to an output stream. The parameter isn this method should always be an integer value.
4. write(byte buffer[ ]) : This method will write a complete array of bytes to an output stream.
|
| FileInputStream Class
|
|
This class will create an InputStream which can be used to read bytes from a file.the two very basic and common constructors of FileInputStream class are :
1. FileInputStream(String filepath)
2. FileInputStream(File fileObj)
They both can throw a FileNotFoundException if the file is not found. The second constructor should be taken into practise as it helps us to examine the file before attaching it to an input Stream.
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String[] args) throws IOException {
char arr[] = new char[100];
FileInputStream fileStream =
new FileInputStream("myexamplefile.txt");
int data;
for (int i = 0; (data = fileStream.read()) > 0; i++) {
arr[i] = (char) data;
}
System.out.println(arr);
}
}
Be sure that the file called "myexamplesfile.txt" exists in the current directory with some contents in it.
|
[edit] FileOutputStream Class
|
|
This class creates an OutputStream which is used to write bytes to a file. This is not dependent on the existing file but it itself creates a file before opening it for the output whenever we create the object.
The basic constructors associated to this class are :
1.FileOutputStream(String filePath):
2. FileOutputStream(File fileObj)
3. FileOutputStream(String filePath, boolean append)
4. FileOutputStream(File fileObj, boolean append)
All of them throw a FileNotFoundException if any error occurs and we get an IOException if we try to access a Read Only File.
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) throws IOException {
String str = "Hello World.";
FileOutputStream fileOutput =
new FileOutputStream("myexamplefile.txt");
fileOutput.write(str.getBytes());
fileOutput.flush();
fileOutput.close();
}
}
|
[edit] ByteArrayOutputStream
|
|
This class is an implementation of an output stream that uses a byte
array as the destination. It has two constructors that are shown below:
1.ByteArrayOutputStream( )
A buffer of 32 bytes is created in this case.
2. ByteArrayOutputStream(int numBytes)
In this case a buffer will be created which will be equal to the size of numBytes and it can be automatically increased if required.
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteArrayOutputStreamExample {
public static void main(String[] args) throws IOException {
// We need to mock up the scenarion for this example. So we
// are mocking up the file creation here but in real time
// it should already be existing.
FileOutputStream filOutputStream = new FileOutputStream("myfile.txt");
filOutputStream.write("Hello Meshplex.".getBytes());
filOutputStream.flush();
filOutputStream.close();
byte b[] = new byte[100];
FileInputStream fileInputStream = new FileInputStream("myfile.txt");
fileInputStream.read(b);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
byteOutputStream.write(b);
// you can either pass this object to any
// method that accept OutStream and then
// need to convert into a byte array. Here we
// have used toString() but you can also
// use toByteArray() that will return you the
// byte array.
System.out.println(byteOutputStream.toString());
}
}
|
[edit] FilterInputStream
|
|
These are just the wrappers which surround the input or output streams depending upon the case whether it is an Input or an Output Stream. It has all the methods which are available in the Input Stream.
The constructor for this is :
FilterInputStream(InputStream is)
|
[edit] FilterOutputStream
|
|
This class ahs all the methods similar to the Output Stream .Its constructor is shown below:
FilterOutputStream(OutputStream os)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
public class FilterInputStreamExample {
public static void main(String[] args) throws FileNotFoundException,
IOException {
byte b[] = "Welcome to Meshplex.".getBytes();
FilterOutputStream filtereOutputStream = new FilterOutputStream(
new FileOutputStream("newFile.txt"));
filtereOutputStream.write(b);
filtereOutputStream.close();
}
}
|
[edit] BufferedInputStream
|
|
This class provides us with the facility that we can wrap any InputStream into a buffered Stream which will help us to improve its performance to a very higher level.
This class has two constructors which are :
1. BufferedInputStream(InputStream inputStream)
This will create a buffer with a default buffer size.
2. BufferedInputStream(InputStream inputStream, int bufSize)
In this case the size of buffer is equal to the variable int bufSize.
This also supports the mark( ) and reset( ) methods. This support is reflected by BufferedInputStream.markSupported( ) returning true.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String[] args) throws FileNotFoundException,
IOException {
// Create new File with some data just for this example to work.
FileOutputStream outFile = new FileOutputStream("buffered.txt");
outFile.write("Hello MeshPlex.".getBytes());
outFile.close();
char buff[] = new char[100];
int value;
BufferedInputStream buffer =
new BufferedInputStream(new FileInputStream("buffered.txt"));
for (int i = 0; (value = buffer.read()) > 0; i++) {
buff[i] = (char) value;
}
System.out.println(buff);
}
}
|
[edit] BufferedOutputStream
|
|
This stream is similar to any other OutputStream.It has an added exception of flush ( ) method which is used to make sure that the data is written physically on the actual output device.They ensure evry high level of performance with the feature of buffering.
It has two constructors available with it which are as under:
1. BufferedOutputStream(OutputStream outputStream)
This creates a buffered stream using the buffer of 512 bytes.
2. BufferedOutputStream(OutputStream outputStream, int bufSize)
This creates the buffer of the size equal to the variable passed in bufSize
|
[edit] PushbackInputStream
|
|
This is implemented on an input stream to provide the access a byte to be read and then returned back to the stream. It works on the mechanisn of pushing inside and then seeking it back.
It has the feature through which we can know what is going to come from an Input Stream.
This class has the following constructors :
.
1. PushbackInputStream(InputStream inputStream)
This allows the creation of a stream which permits one byte to be returned to the input Stream.
2. PushbackInputStream(InputStream inputStream, int numBytes)
This creates a stream that has a pushback buffer that is equal to the length of numBytes long. This allows multiple bytes to be returned to the input stream.
|
[edit] SequenceInputStream
|
|
This class provides us the feature that we can concatenate the multiple InputStreams. It differs a lot from any other InputStream when there is a caparison between any InputStream and SequenceInputStream.
It contains the following constructors :
1. SequenceInputStream(InputStream first, InputStream second)
Here, First and second are the two InputSteams that will be concatenated i.e. combined together to give a new InputStream as the output of this operation.
2. SequenceInputStream(Enumeration streamEnum)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
public class SequenceInputStreamExample {
public static void main(String[] args) throws FileNotFoundException,
IOException {
FileOutputStream fileOutput1 = new FileOutputStream("file1.txt");
fileOutput1.write("This is my first file.".getBytes());
fileOutput1.close();
FileOutputStream fileOutput2 = new FileOutputStream("file2.txt");
fileOutput2.write("This is my second file.".getBytes());
fileOutput2.close();
FileInputStream fileIn1 = new FileInputStream("file1.txt");
FileInputStream fileIn2 = new FileInputStream("file2.txt");
SequenceInputStream sequenceInputStream =
new SequenceInputStream(fileIn1, fileIn2);
BufferedReader reader =
new BufferedReader(new InputStreamReader(sequenceInputStream));
System.out.println(reader.readLine());
}
}
|
[edit] PrintStream
|
|
PrintStream class can be said to be the class which is very much responsible for all type of formatting capabilities which we need to use while working from the System file handle, System.out. It supports both print( ) and println( ) methods for all types, including the Object too.
It has the following two constructors:
1.PrintStream(OutputStream outputStream)
2.PrintStream(OutputStream outputStream, boolean flushOnNewline)
In this flushOnNewline keeps a check as to whether Java will flush the output stream every time we get a newline (\n) character as the output. It will perform the flush operation if it has Boolean value as True” else will not do anything as it gets the value as “false”.
|
| DataInputStream
|
|
|
| DataOutputStream
|
|
|