Java:InputStream读取大文件太慢了

我必须按字符读取53 MB的文件。 当我使用ifstream在C ++中完成它时,它在几毫秒内完成,但使用Java InputStream需要几分钟。 Java很慢或者我错过了什么是正常的吗?

另外,我需要用Java完成程序(它使用servlet,我必须从中调用处理这些字符的函数)。 我想也许用C或C ++编写文件处理部分,然后使用Java Native Interface将这些函数与我的Java程序连接……这个想法怎么样?

任何人都可以给我任何其他提示……我真的需要更快地阅读文件。 我尝试使用缓冲输入,但它仍然没有提供甚至接近C ++的性能。

编辑:我的代码跨越了几个文件,它非常脏,所以我给出了概要

import java.io.*; public class tmp { public static void main(String args[]) { try{ InputStream file = new BufferedInputStream(new FileInputStream("1.2.fasta")); char ch; while(file.available()!=0) { ch = (char)file.read(); /* Do processing */ } System.out.println("DONE"); file.close(); }catch(Exception e){} } } 

我用183 MB文件运行此代码。 它印有“Elapsed 250 ms”。

 final InputStream in = new BufferedInputStream(new FileInputStream("file.txt")); final long start = System.currentTimeMillis(); int cnt = 0; final byte[] buf = new byte[1000]; while (in.read(buf) != -1) cnt++; in.close(); System.out.println("Elapsed " + (System.currentTimeMillis() - start) + " ms"); 

我会尝试这个

 // create the file so we have something to read. final String fileName = "1.2.fasta"; FileOutputStream fos = new FileOutputStream(fileName); fos.write(new byte[54 * 1024 * 1024]); fos.close(); // read the file in one hit. long start = System.nanoTime(); FileChannel fc = new FileInputStream(fileName).getChannel(); ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); while (bb.remaining() > 0) bb.getLong(); long time = System.nanoTime() - start; System.out.printf("Took %.3f seconds to read %.1f MB%n", time / 1e9, fc.size() / 1e6); fc.close(); ((DirectBuffer) bb).cleaner().clean(); 

版画

 Took 0.016 seconds to read 56.6 MB 

使用BufferedInputStream

 InputStream buffy = new BufferedInputStream(inputStream); 

如上所述,使用BufferedInputStream。 您也可以使用NIO包。 请注意,对于大多数文件,BufferedInputStream将与NIO一样快速读取。 但是,对于非常大的文件,NIO可能会做得更好,因为您可以进行内存映射文件操作。 此外,NIO包执行可中断的IO,而java.io包则不执行。 这意味着如果要从其他线程取消操作,则必须使用NIO使其可靠。

 ByteBuffer buf = ByteBuffer.allocate(BUF_SIZE); FileChannel fileChannel = fileInputStream.getChannel(); int readCount = 0; while ( (readCount = fileChannel.read(buf)) > 0) { buf.flip(); while (buf.hasRemaining()) { byte b = buf.get(); } buf.clear(); }