简体   繁体   中英

Inputstream and Outputstream with reading a list of files

I have this ArrayList files

for(File file : files){

    InputStream in = FileInputStream(file);
    // process each file and save it to file
    OutputStream out = FileOutputStream(file);
    try{

    } finally {
       in.close();
       out.close();
    }
}

the performance is really slow since every loop there is a in/out close(), is there a better way to do this? I tried to put outputstream oustide of the loop, it doesn't work.

A close() can take up to 20 ms. I doubt this is your program unless you have 1000's of files.

I suspect your performance problem is a lack of buffering the input and output. Can you show your buffering wrappers as well?

Using buffered streams makes a huge difference.

Try this:

for(final File file : files) {

    final InputStream in = new BufferedInputStream(new FileInputStream(file));
    final OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(...)));
    try {
        // Process each file and save it to file
    }
    finally {
        try {
            in.close();
        }
        catch (IOException ignored) {}
        try {
            out.close();
        }
        catch (IOException ignored) {}
    }
}

Note that the IOException s that can be thrown when closing the streams must be ignored, or you will lose the potential initial exception.

Another problem is that both streams are on the same file, which doesn't work. So I suppose you're using two different files.

you can of course build a queue of OutputStreams and offload that to a background thread that handles the closing of these outputstreams. Same for InputStreams. Alternatively you can leave it down to the JVM to do that -- simply don't close the files and leave it to the GC to do that when objects are finalized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM