Académique Documents
Professionnel Documents
Culture Documents
prequel1.blogspot.ae
Contents
1 2014 1.1 January . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integration With Syntax Highlighter Test (2014-01-29 23:01) . . . . . . . . . . . . . . . . . 1.2 February . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Who Am I ? (2014-02-04 03:16) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JAVA IO Beginner (2014-02-04 03:42) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . JAVA IO Beginner - Part 1 (2014-02-05 23:54) . . . . . . . . . . . . . . . . . . . . . . . . . How To Convert A File To A Byte Array? (2014-02-06 00:29) . . . . . . . . . . . . . . . . . How to Convert A Byte Array to Image? (2014-02-06 01:58) . . . . . . . . . . . . . . . . . 5 5 5 5 5 6 6 7 8
Chapter 1
2014
1.1 January
1.2
February
1. What are streams? 2. What are character streams and byte streams? 3. What is the dierence between character based streams and byte streams? 4. What is a dierence between InputStream/OutputStream and Reader/Writers? 5. How to convert a le to a byte array? 6. How to read from a le? 7. How to write to a le? 8. Converting an image to byte array and vice versa? 9. What is a Random Access File? 10. How to read and write to a Random Access File? 11. How to build an Address book Application using low-level File IO API? If you also nd diculties in understanding these concepts, here you will nd the answers related to them. The rst four questions will be covered in one article which will be more theory based. Rest i will be putting all coding samples. Lets gets started :Prequel
1. What are streams? 2. What are character streams and what are byte streams? 3. What is the dierence between byte based and character based streams? 4. What is the dierence between InputStream/OutputStream and Reader/Writer? 6
I am a beginner and learner myself and i would try to answer the questions as best to my knowledge. I would also be including articles i referred at the end of every post. Concept of Stream Well, a stream is nothing but a data which we access in sequence. Well all know that in todays modern age, we come across dierent types of data. Data can be anything, for example an image, a sound le, a text le, spread sheet and so on. I would term these dierent types of data as Data Format. When the format of data is dierent, the means of accessing that data would be dierent too. Dierence Between Character Based and Byte Based Streams Please remember that on the broader context, everything in case of computers at the lowest level is a byte. Now how to decode or encode that data makes the concept of character and byte based streams. Just dont confuse yourself, when we are talking about text format, it is also a byte, but obviously a decoding or encoding mechanism is needed to understand the information. That encoding/decoding makes the concept of character based streams alive. Byte stream is suitable for any kind of data but in order to understand dierent format from this byte stream, a byte stream that has been wrapped with a logic to output characters is called a Character Based Stream. It also answers the point 2 & 3 . What is InputStream/OutputStream and Reader/Writers In order to understand, point-4, rst we need to know what is an InputStream and what is an OutputStream. An InputStream is a raw method to extract information from a resource. It grabs the data byte by byte without applying any kind of translation on it. If we are trying to read format like an image, or any binary le, this is the format to use. OutputStream on the other hand is a raw method to write information to a stream byte by byte without applying any kind of encoding scheme. When talked about data in dierent formats, we obviously need dierent handlers capable for handling dierent formats of data. Readers are specially designed for character based streams. If the information you are reading is purely text, than Reader will take care of character decoding for you and give you uni-code characters from the InputStream. Same holds true for Writers. They will write character based data into OutputStream taking care of character encoding for you. As a gotcha, we can turn an InputStream into a reader as follows :Reader reader = new InputStreamReader(inputStream);
I hope this article helped you answer the basic questions highlighted in this article. It will help the beginner in order to move forward with the concepts. Prequel
cases, this scenario comes in case of image les. So lets see which class in java.io package gives us this facility. When i looked into the java.io package, most close class which can give us a byte array is ByteArrayOutputStream. So whats a ByteArrayOutputStream. Let me quote directly from the java documentation. This class implements an output stream in which the data is written into a byte array. The buer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString(). Closing a ByteArrayOutputStream has no eect. The methods in this class can be called after the stream has been closed without generating an IOException. So, if we read a le and write data byte by byte into ByteArrayOutputStream. Later we can read that data using its to toByteArray(). Here is a small code snippet to demonstrate this concept. public static void main(String[] args) throws IOException { //create buffer capacity of specific size. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(3000); //Read the file FileInputStream fis = new FileInputStream(new File("d:\\image.jpg")); //Write bytes into ByteArrayOutputStream int c ; while ((c = fis.read()) != -1 ){ byteArrayOutputStream.write(c); } //flush() the outputstream byteArrayOutputStream.flush(); //Get the byteArray byte[] bytes = byteArrayOutputStream.toByteArray(); }
This program simply demonstrates the important concepts. If any confusions, please feel free to shoot the email to me. Prequel
Returns a BueredImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. [4]write([5]RenderedImage im, [6]String formatName, [ 7]File output) Writes an image using an arbitrary ImageWriter that supports the given format to a File. So if we try this code, we would be able to convert a Byte Array to image le. import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; /** * @author Prequel * This class is responsible for converting an image to a byte array * and coverting that byte array back to Image. */ public class ImageTest { public static void main(String[] args) throws IOException { //Step -1 allocation size of ByteArrayOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(3000); //Step 2- Reading Image File FileInputStream fis = new FileInputStream(new File("d:\\image.jpg")); //Step 3 - Writng Image File to an ByteArrayOutputStream int c ; while ((c = fis.read()) != -1 ){ byteArrayOutputStream.write(c); } byteArrayOutputStream.flush(); //Step-4 Converting to a ByteArray byte[] bytes = byteArrayOutputStream.toByteArray(); //Step-5 Coverting Byte Array to a BufferedImage BufferedImage bufferedImage1 = ImageIO.read(new ByteArrayInputStream(bytes)); //Step-6 Converting BufferedImage to a Image File ImageIO.write(bufferedImage1 , "jpg" , new File("c:\\snap1.jpg")); } }
The above concept is simple to understand. Though the above code has introduced a new concept of a BueredImage. Though not relevant, here is a quick introduction about BueredImage. What is a BueredImage? If we look close into the documentation, we would know that Imageis an abstract class, so we cant directly instantiate it. BueredImage is a subclass of Image, providing concrete functionality which can done 9
in case of an Image. If you want to look at the close discussion about this concept. Here is a link from stackoverow [8]Dierence Between Image and BueredImage. Thanks, Prequel
1. http://prequel1.blogspot.ae/2014/02/how-to-convert-file-to-byte-array.html 2. http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read(java.io.InputStream) 3. http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html 4. http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#write(java.awt.image.RenderedImage, java.lang.String,java.io.File) 5. http://docs.oracle.com/javase/7/docs/api/java/awt/image/RenderedImage.html 6. http://docs.oracle.com/javase/7/docs/api/java/lang/String.html 7. http://docs.oracle.com/javase/7/docs/api/java/io/File.html 8. http://stackoverflow.com/questions/3944825/difference-between-the-image-and-bufferedimage-in-java
10