Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a large file in Java is very slow

I have a large (12GB) file and I need to extract small pieces of data (a few kilobytes each) from it, using Java. Seeking and reading the data, once the file is open, is very fast, but opening the file itself takes a long time - about 90 seconds. Is there a way to speed up the open file operation in Java?

To clarify, I've tried the following options to open and read a file:

new FileInputStream(file); 
new RandomAccessFile(file, "r");
Files.newByteChannel(path, StandardOpenOption.READ);

Each one of these yielded similar results.

like image 613
Little Bobby Tables Avatar asked Dec 10 '12 10:12

Little Bobby Tables


People also ask

Which of the following is the best method to write large amount of data to a file?

The best solution would be implement own Writer which directly uses write(byte[]) method of FileOutputStream which used underlying native writeBytes method .


2 Answers

From the commments: To be specific, the problem is that Java's open file operation triggers the OS operation that runs the virus scan, and the solution is to add Java to the list of trusted processes

like image 135
Stefan Avatar answered Oct 23 '22 06:10

Stefan


The problem you have is mostly caused by JNI you are using.

As your code wait during constructor for FileInputSream(String). That veryfie the existance of passed path and call a method private native void open(String).

Then openJDK implementation of FileInputSream#open(String) look like:

    JNIEXPORT void JNICALL
    Java_java_io_FileInputStream_open(JNIEnv *env, jobject this, jstring path) {
        fileOpen(env, this, path, fis_fd, O_RDONLY);
    }

This move us to io_util_md.c and method

jlong winFileHandleOpen(JNIEnv *env, jstring path, int flags)

You can analyse the code there.


At this point you have various options.

  • Check the different JDK
  • Write a C code, to creaete onw JNI method.
  • Check the differnet file system.
like image 1
Damian Leszczyński - Vash Avatar answered Oct 23 '22 05:10

Damian Leszczyński - Vash