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();
}
}
}
这两种方法有啥区别?你把p1和p2方法换个位置试试看。
p1方法并没有将文件读取完整,而方法二读取完整了