import java.io.*;
import java.io.File;
import java.io.FileFilter;
import java.lang.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class test
{
public static void main(String[] args) throws Exception
{
InputStream in = null;
long s = System.currentTimeMillis();
try{
byte[] tempbytes = new byte[8192];
int byteread = 0;
in = new FileInputStream("test.txt");
while ((byteread = in.read(tempbytes)) != -1){
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null){
try {
in.close();
} catch (IOException e1) {
}
}
}
long e = System.currentTimeMillis() - s;
System.out.println("\nTime spend: " + e + "\t Per: " + (1.0 * e / 1000));
}
}
------最佳解决方案--------------------------------------------------------
如果你直接用C来写 64bit 应用的话,应该可以。
但为什么要把2G全部放内存中??
另外,我在我的电脑上完全测不出来你的效果,我觉得神了个奇的。。。
我还特地打开了GC日志(-XX:+PrintGCDetails),想看看有没有内存频繁回收的问题,结果1次GC都没有!
这是我程序:
public static void scan(String filename) throws IOException {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
try {
long timer = System.currentTimeMillis();
byte[] buff = new byte[32 * 1024];
while (fis.read(buff) >= 0) {
// Do nothing
}
timer = System.currentTimeMillis() - timer;
System.out.printf("Readed: %,dKB\t\tSpend: %dms\t\tPerMS: %dKB\n", f.length() / 1024, timer, f.length() / 1024 / timer);
} finally {
fis.close();
}
}