Monday, April 15, 2013

Java IO vs NIO

Java NIO (New IO) was introduced in JDK 1.4. While Java IO centralizes around Stream/Reader/Writer and uses decorator pattern as its main design, where you have decorate one InputStream type with each other in the right order. The NIO uses Channel/Buffer/Selector.

Read a file using IO

File file =new File("C:\\test\\test.txt");
FileInputStream  fis = new FileInputStream(file);

//decorate BufferedReader with InputStreamReader
BufferedReader d = new BufferedReader(new InputStreamReader(fis));
String line=null;
while((line = d.readLine()) != null){

Read a file using NIO

RandomAccessFile rFile = new RandomAccessFile("C:\\test\\test.txt", "rw");
//get Channel
FileChannel inChannel = rFile.getChannel();
//Intialize byte buffer
ByteBuffer buf = ByteBuffer.allocate(100);
//read data to buffer
 buf.flip(); //flip into read mode


  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

  2. Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

  3. A befuddling web diary I visit this blog, it's incredibly grand. Strangely, in this present blog's substance made motivation behind fact and sensible. The substance of information is instructive
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

  4. This is really a wonderful post. First, I would rather thank the stress of golf but the connection plays a leading role in the accommodation. Fantastic stuff.

  5. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. r&b musica italian

  6. Your Affiliate Money Making Machine is ready -

    Plus, making money online using it is as simple as 1 . 2 . 3!

    Here's how it works...

    STEP 1. Choose affiliate products the system will promote
    STEP 2. Add PUSH BUTTON TRAFFIC (it LITERALLY takes JUST 2 minutes)
    STEP 3. See how the affiliate products system explode your list and sell your affiliate products all on it's own!

    Are you ready??

    Click here to launch the system

  7. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
    ibm full form in india |
    ssb ka full form |
    what is the full form of dp |
    full form of brics |
    gnm nursing full form |
    full form of bce |
    full form of php |
    bhim full form |
    nota full form in india |
    apec full form