Vous êtes sur la page 1sur 12

From Blog to Book.

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

Integration With Syntax Highlighter Test (2014-01-29 23:01)


Testing syntax highlightening public static void main(String[] args){ }

1.2

February

Who Am I ? (2014-02-04 03:16)


Hi Everyone, This is my rst post on my blog which was started earlier this month. If you are on this blog, you must know who am i. My name is Prequel. Welcome on board. The blog signies discovery as Prequel means a discoverer. The mantra of my story is, no one knows everything in this life span. We all are part of a bigger picture. That is life. What keeps our spirit alive is the curiosity in our nature and discovery about unknown. I am an Amateur developer. I read about technology, write about life and practice a bit spirituality. I am not an expert in any eld. I just take life in a normal ow. On this blog, you will nd articles regarding Java, my personal goals and achievements. Thats all about me at this moment. If you want to know more about me, connect me via email. prequel.java@gmail.com I might not be able to answer every email, but i will try my best. Thanks, Prequel

JAVA IO Beginner (2014-02-04 03:42)


Hi Everyone, I am new to Java and try to learn the bit and tricks as well as in depth. This is a corner for people who have just started learning Java. I would be posting here articles regarding to core and basically concepts behind major foundations. This week, i am learning about Java IO and i will be posting small programs i developed through out my learning paradigm. I plan to cover the following topics completely in one article and i would putting up sample source code of an Address Book Application through RandomAccessFiles. For a JAVA IO beginner like me, i found these questions pretty tricky. Here is a list of sample questionnaire i jotted down during my learning paradigm.

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

JAVA IO Beginner - Part 1 (2014-02-05 23:54)


Hello, Welcome to Java IO Beginner - Part -1. In this article, i will be completing the following concepts :-

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

How To Convert A File To A Byte Array? (2014-02-06 00:29)


Dear Beginner, Are you nding confusion that the IO package is so huge, which classes to use in which scenario? I must tell you, I am also a beginner and nding answers to these questions. The best answer, i get from inner self is CODE CODE CODE and you will know the answer. I found an excellent article which i will share later. Right now, here is a small requirement sometimes, we need to convert a le to Byte Array. Most of the 7

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

How to Convert A Byte Array to Image? (2014-02-06 01:58)


Dear Beginner, Have you encountered a requirement to covert a Byte Array to an image? If yes, this article will tell you a simpler way to do so. This article is continuation of [1]Converting File to Byte Array. Okay, so you have a Byte Array now and you want to convert it into an Image. If we glance into package of javax.imageio, we get a utility class ImageIO, that can help up converting a byte Array to Image. ImageIO is a utility class that contains static methods for ImageReaders and ImageWriters. Here are the two methods that can help up achieve this task. Directly from Java documentation. [2]read([3]InputStream input) 8

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

BlogBook v0.4, EX 2 & GNU/Linux. http://www.blogbooker.com


A L T

Edited: February 6, 2014

Vous aimerez peut-être aussi