Sunday, January 5, 2020
How to Read and Write Byte Streams in Java
Reading and writing binary streams is one of the most common I/O tasks a Java application can perform. It can be performed by looking at each individual byte in a stream or by using a more structured buffered approach. Note: This article looks at reading binary data from a example.jpg file. If you try this code then simply replace the name of the example.jpg with the path and name of a jpeg file on your computer. Byte by Byte The java.ioclass was the first Java api to provide Input/Output functionality. It has two methods that can be used to input and output byte streams (blocks of 8 bits) from and to a file. These classes are the FileInputStream and FileOutputStream. These methods provide a basic method of I/O by allowing a file to be input or output one byte at a time.à In practice its better to use a buffered method for binary streams but its good to look at the most basic building block of the Java I/O functionality. Notice how we place the I/O handling inside a try, catch, finallyblockââ¬âthis is to make sure we handle IO exceptions and to properly close the streams. The catch block will show any I/O exceptions that occur and print a message for the user. In the finally block its important to close the streams explicitly by calling the close method otherwise they will remain open and a waste of resources. There is a check to see if the FileInputStreamand FileOutputStreamare null before attempting to close. This is because an I/O error could occur before the streams are initialized. For example, if the file name is incorrect the stream will not be opened properly.In the tryblock we can add code to read in the bytes:The readmethod reads in one byte from the FileInputStreamand the write method writes one byte to the FileOutputStream. When the end of the file is reached and there are no more bytes to input the value of -1 is returned. Now that Java 7 has been released you can see the benefit of one of its new featuresââ¬âthe try with resources block. This means that if we identify the streams to the try block at the beginning it will handle closing the stream for us. This eliminates the need for the finally block in the previous example: The full Java code listings for the two versions of the byte reading program can be found in Binary Stream Example Code.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.