Session 10 |
I/O streams
A stream is a sequence of data. The base classes for I/O streaming are the abstract classes InputStream
and OutputStream
, and later these classes are extended to to have some added functionality. They can be categorized intuitively as follows.
Byte streams
Byte Stream classes are mainly used to handle byte-oriented I/O. It is not restricted to any particular data type, though, and can be used with objects including binary data. The data is translated into 8-bit bytes for I/O operations. This makes byte stream classes suitable for I/O operations where a specific data type does not matter and can be dealt with in binary form as well. Byte Stream classes are mainly used in network I/O such as socket or binary file operation. To open a byte stream, use the following statements:
InputStream myInput = new FileInputStream("myPicture.jpg"); OutputStream myOutput = new FileOutputStream("mySecondPicture.jpg");
Some useful methods of the abstract classes are:
int read()
- reads and returns the next byte of data from the input stream;int read(byte[] buf)
- reads 1 <= n <= buf.length bytes from the input stream and stores them into the buffer array;void close()
- closes the input stream and releases any system resources associated with the stream.
void write(int b)
- writes the specified byte to the output stream;void write(byte[] buf, int offset, int length)
- writes length bytes from the specified byte array buf starting at offset offset to the output stream;void close()
- closes the output stream and releases any system resources associated with this stream.
The following example demonstrates manipulations with byte streams. Namely, the program makes a copy of the file. The method myCopy
reads bytes from the input byte stream and places each byte into the output stream. (Note that reading byte by byte is not efficient; therefore, the number of the bytes read at a time is increased to 1024).
//This program makes a copy of the given file: import java.io.*; public class CopyMyFile { private static void myCopy(String myFile, String myCopy) throws Exception { InputStream myInput = new FileInputStream(myFile); OutputStream myOutput = new FileOutputStream(myCopy); byte[] myBuffer = new byte[1024]; int myRead = myInput.read(myBuffer); while (myRead > 0) { myOutput.write(myBuffer, 0, myRead); // only the part that has data myRead = myInput.read(myBuffer); // read the next 1024 bytes } myInput.close(); myOutput.close(); } public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Have you entered a file name?"); System.exit(1); } myCopy(args[0], args[0] + ".copy"); } }
If you use IntelliJ, add the program arguments as follows: Run
-> Edit Configurations
-> Configuration
tab -> Program arguments
.
Why can't we make a copy of the whole file at once? The problem is connected to the file size. If a file is too large, the RAM will get full during while reading. Reading a file by portions eliminates the problem of the memory overflow.
Session 10 |