同樣的讀取方法,為什沒p1方法比p2快了很多?
package TestDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//批量讀取
public class InputDemo1 {
public static ?void ?p1(String ?fileName) throws IOException{
File file1= new ?File(fileName);
FileInputStream fis= new ?FileInputStream(file1);
byte[] byte1=new ?byte[20*1024];
int b=fis.read(byte1, 0, byte1.length);
int temp=1;
for(int i=0;i<b;i++){
System.out.print(Integer.toHexString(byte1[i]&0xff)+" ?");
if(temp++%10==0){
System.out.println();
}
}
fis.close();
}
public static void ?p2(String fileName) throws IOException{
File file2= new ?File(fileName);
FileInputStream fis2=new ?FileInputStream(file2);
byte[] byte2=new ?byte[20*1024];
int k=0;
int temp2=1;
while((k=fis2.read(byte2, 0, byte2.length))!=-1){
for (int i = 0; i <k; i++) {
System.out.print(Integer.toHexString(byte2[i]&0xff)+" ");
if(temp2++%10==0){
System.out.println();
}
}
}
fis2.close();
}
public static void main(String[] args) {
try {
long start=System.currentTimeMillis();
p1("E:\\JavaWeb.zip");
//p2("E:\\JavaWeb.zip");
long end=System.currentTimeMillis();
System.out.println((start-end)+"ms");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2015-12-15
這兩種方法有啥區(qū)別?你把p1和p2方法換個位置試試看。
2015-10-11
p1方法并沒有將文件讀取完整,而方法二讀取完整了