Session 10 |
Some tricks with streams
Streams can read data from different places
If some data have to be read from the console, Java standard I/O streams can be used:
InputStream myInput = System.in; OutputStream myOutput = System.out;
To read data from the Internet, use class URL
:
InputStream myInput = new URL("http://ut.ee/").openStream();
Classes ZipInputStream
and ZipOutputStream
can be used for reading/writing data from/into zip files. Here is an example of zipping a file:
import java.io.*; import java.util.zip.*; public class Zipper { public static void toZip(String fileToZip, String zipFile) throws Exception { ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); FileInputStream inputFile = new FileInputStream(fileToZip); // prepare for zipping zipOut.putNextEntry(new ZipEntry(fileToZip)); // read from the file and write into zip file byte[] buf = new byte[1024]; int len; while ((len = inputFile.read(buf)) > 0) { zipOut.write(buf, 0, len); // (data, offset, length) } inputFile.close(); zipOut.closeEntry(); zipOut.close(); } }
ByteArrayOutputStream
Sometimes it is more convenient to process data in one unit, not in a stream. If some data are available only as a byte stream, it is possible to put all the bytes into byte[]
. The ByteArrayOutputStream
class helps merge the bytes:
InputStream myStream = new FileInputStream("myPicture.jpg"); ByteArrayOutputStream myCollector = new ByteArrayOutputStream(); // collect all bytes here byte[] buf = new byte[1024]; int len; while ((len = myStream.read(buf)) > 0) { // check and assignment as a loop expression myCollector.write(buf, 0, len); // add the next portion of bytes } myStream.close(); byte[] myFile = myCollector.toByteArray(); // all bytes as one unit
There is an analogical class for the output - ByteArrayInputStream
.
Be careful - a file might not fit into the RAM!
Do not forget to close streams
Each stream has a close
method. If this method is not used after the program has finished its work with the resource, some weird cases may happen: the buffered output stream may not return the data from the buffer, the operating system can limit the number of the opened files or block the file. Therefore, do not forget to close the resource!
InputStream and OutputStream are not the only input and output streams
We have discussed classes InputStreamReader
and OutputStreamWriter
. These are the subclasses of the abstract classes Reader
and Writer
. However, Reader
and Writer
form another I/O hierarchy which operate on characters.
Do not forget to set the character set
Whenever an input/output stream is used, set the character encoding. However, some classes like FileReader
and FileWriter
do not allow to set the encoding and use the encoding set of the operating system. Try to avoid such classes since the default encoding of the operating system may not be UTF-8 - this may cause problems on other computers when the data are read or written. Use InputStreamReader
and OutputStreamWriter
instead of the FileReader
and FileWriter
classes.
Length of the stream is not set
An input stream is a structure from where bytes are read. The stream is endless; therefore, the input stream does not have methods like size
or length
which would help define the number of the left bytes. On the other hand, the stream has method available()
.
Session 10 |