首页 / JAVA / Java.io:性能调优
Java.io:性能调优
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java.io:性能调优,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2204字,纯文字阅读大概需要4分钟。
内容图文
![Java.io:性能调优](/upload/InfoBanner/zyjiaocheng/808/82190877eed144bebc617e37f44ee24b.jpg)
我有一个大约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毫秒,但在这种情况下,这是过度杀死.
内容总结
以上是互联网集市为您收集整理的Java.io:性能调优全部内容,希望文章能够帮你解决Java.io:性能调优所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。