Jayesh
Jayesh

Reputation: 6111

IO and NIO performance difference and example

I am very new to Java NIO and not have hands on it. Regarding Java NIO what I know is it is fast then java.IO.

So, just to give a try I thought of writing simple programs for "copying contents of one file to another". "Search a word from large file".

using both java.io and java.nio package.

Also, I have printed time before and after operations start and end respectively.

I didn't found any difference as such that NIO is faster. Might be I am going in wrong direction.

Can anyone please guide me through scenarios where I can properly see the difference through example?

EDIT:

I am really surprised to know that this question will get negative vote. I have mentioned that I am new to NIO and guide me if am going in wrong direction. I have not post a program because it was very basic read-write operation...please see below program i used to test....

Using IO

public static void copyFile(File in, File out) throws Exception {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date now = new Date();
    String strDate = sdf.format(now);

    System.out.println("Before Read :"+strDate);


    FileInputStream fis  = new FileInputStream(in);
    FileOutputStream fos = new FileOutputStream(out);
    try {
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    } 
    catch (Exception e) {
        throw e;
    }
    finally {
        if (fis != null) fis.close();
        if (fos != null) fos.close();
    }

    Date now1 = new Date();
    String strDate1 = sdf.format(now1);

    System.out.println("After Read :"+strDate1);


}

Using NIO

 public static void copyFile(File in, File out) 
        throws IOException 
{

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date now = new Date();
    String strDate = sdf.format(now);

    System.out.println("Before Read :"+strDate);

    FileChannel inChannel = new
        FileInputStream(in).getChannel();
    FileChannel outChannel = new
        FileOutputStream(out).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(),
                outChannel);
    } 
    catch (IOException e) {
        throw e;
    }
    finally {
        if (inChannel != null) inChannel.close();
        if (outChannel != null) outChannel.close();
    }

    Date now1 = new Date();
    String strDate1 = sdf.format(now1);

    System.out.println("After Read :"+strDate1);
}

File which I have gave to copy from one file to another was around 20 MB.

Upvotes: 3

Views: 7736

Answers (2)

Reegan Miranda
Reegan Miranda

Reputation: 2949

NIO allows you to manage multiple channels using only a single (or fewer) threads, but the cost is that parsing the data might be somewhat more complicated than when reading data from a blocking stream using standard IO.

If you need to manage thousands of open connections simultaneously, which each only send a little data, for instance a chat server, implementing the server in NIO is probably an advantage. Similarly, if you need to keep a lot of open connections to other computers, e.g. in a P2P network, using a single thread to manage all of your outbound connections might be an advantage.

If you have fewer connections with very high bandwidth, sending a lot of data at a time, standard IO server implementation should be your choice.

Ref : Difference between standard IO and NIO

Upvotes: 5

user93353
user93353

Reputation: 14039

It isn't really true that NIO is faster. Paul Tyma demolished that myth sometime back.

http://mailinator.blogspot.in/2008/02/kill-myth-please-nio-is-not-faster-than.html

http://paultyma.blogspot.in/2008/03/writing-java-multithreaded-servers.html

Upvotes: 3

Related Questions