我有一个大约4MB的文件,该文件只是一个包含普通键盘字符的ascii文件.我在java.io包中尝试了很多类来将文件内容读作字符串.逐个字符地读取它们(使用FileReader和BufferedReader)
大约40秒,使用java.nio包(FileChannel和ByteBuffer)读取内容大约需要25秒.据我所知,这有点大
多少时间.有人知道如何减少这种时间消耗
大约10秒钟?甚至像使用C创建文件阅读器和从java调用这样的解决方案也可以.我使用下面的代码片段在22秒内读取4 MB文件 –
public static String getContents(File file) {
try {
if (!file.exists() && !file.isFile()) {
return null;
}
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(512);
Charset cs = Charset.forName("ASCII");
StringBuilder sb = new StringBuilder();
int rd;
while ((rd = ch.read(buf)) != -1) {
buf.rewind();
CharBuffer chbuf = cs.decode(buf);
for (int i = 0; i < chbuf.length(); i++) {
sb.append(chbuf.get());
}
buf.clear();
}
String contents = sb.toString();
System.out.println("File Contents:\n"+contents);
return contents;
} catch (Exception exception) {
System.out.println("Error:\n" + exception.getMessage());
return null;
}
}
解决方法:
我无法想象你的硬件是什么,但4 MB文件应该不到0.1秒.
一次读取文件的快速方法是将其读入byte []
public static String readFileAsString(File file) {
try {
DataInputStream in = new DataInputStream(FileInputStream(file));
byte[] bytes = new byte[(int) file.length()];
in.readFully(bytes);
in.close();
return new String(bytes, 0); // ASCII text only.
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
System.out.println("Error:\n" + e.getMessage());
return null;
}
}
public static void main(String... args) throws IOException {
File tmp = File.createTempFile("deleteme", "txt");
tmp.deleteOnExit();
byte[] bytes = new byte[4 * 1024 * 1024];
Arrays.fill(bytes, (byte) 'a');
FileOutputStream fos = new FileOutputStream(tmp);
fos.write(bytes);
fos.close();
long start = System.nanoTime();
String s = readFileAsString(tmp);
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to read a file with %,d bytes%n",
time / 1e9, s.length());
}
版画
Took 0.026 seconds to read a file with 4,194,304 bytes
如果你想更快地读取文件,我建议使用内存映射文件,因为它将花费不到10毫秒,但在这种情况下,这是过度杀死.