Ege Hurturk
Ege Hurturk

Reputation: 953

Java IO outperforms Java NIO when it comes to file reading

I believed that the new nio package would outperform the old io package when it comes to the time required to read the contents of a file. However, based on my results, io package seems to outperform nio package. Here's my test:


import java.io.*;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class FileTestingOne {

    public static void main(String[] args) {
        long startTime = System.nanoTime();

        File file = new File("hey2.txt");
        try {
            byte[] a = direct(file);
            String s = new String(a);
        }
        catch (IOException err) {
            err.printStackTrace();
        }
        long endTime   = System.nanoTime();
        long totalTime = (endTime - startTime);
        System.out.println(totalTime);
    }

    public static ByteBuffer readFile_NIO(File file) throws IOException {
        RandomAccessFile rFile = new RandomAccessFile(file.getName(), "rw");
        FileChannel inChannel = rFile.getChannel();

        ByteBuffer _buffer = ByteBuffer.allocate(1024);
        int bytesRead = inChannel.read(_buffer);
        while (bytesRead != -1) {
            _buffer.flip();
            while (_buffer.hasRemaining()) {
                byte b = _buffer.get();
            }
            _buffer.clear();
            bytesRead = inChannel.read(_buffer);
        }

        inChannel.close();
        rFile.close();
        return _buffer;
    }

    public static byte[] direct(File file) throws IOException {
        byte[] buffer = Files.readAllBytes(file.toPath());
        return buffer;
    }

    public static byte[] readFile_IO(File file) throws IOException {

        byte[] _buffer = new byte[(int) file.length()];
        InputStream in = null;

        try {
            in = new FileInputStream(file);
            if ( in.read(_buffer) == -1 ) {
                throw new IOException(
                        "EOF reached while reading file. File is probably empty");
            }
        }
        finally {
            try {
                if (in != null)
                    in.close();
            }
            catch (IOException err) {
                // TODO Logging
                err.printStackTrace();
            }
        }
        return _buffer;
    }



}

// Small file
//7566395  -> readFile_NIO
//10790558 -> direct
//707775   -> readFile_IO

// Large file
//9228099  -> readFile_NIO
//737674   -> readFile_IO
//10903324 -> direct

// Very large file
//13700005  -> readFile_NIO
//2837188   -> readFile_IO
//11020507  -> direct

Results are:

I wanted to ask this question because (I believe) nio package is non-blocking, thus it needs to be faster, right?

Thank you,

Edit:

Changed ms to ns

Upvotes: 0

Views: 515

Answers (1)

divisionby0
divisionby0

Reputation: 170

Memory mapped files (or MappedByteBuffer) are a part of Java NIO and could help improve performance.

The non-blocking in Java NIO means that a thread does not have to wait for the next data to read. It does not necessarily affect performance of a full operation (like reading and processing a file) at all.

Upvotes: 1

Related Questions