out.write(buf, 0, b);//這個b什么意思?寫b個長度的字節嗎?但是沒有給b賦值啊
public static void copyFile(File scrFile,File destFile) throws IOException{
if(!scrFile.exists()){
throw new IllegalArgumentException("文件"+scrFile+"不存在");
}
if(!scrFile.isFile()){
throw new IllegalAccessError(scrFile+"不是文件");
? ? ? ?}
FileInputStream in = new FileInputStream(scrFile);
FileOutputStream out = new FileOutputStream(destFile);
byte [] buf = new byte[8*1024];
int b;
while((b=in.read(buf,0,buf.length))!=-1){
out.write(buf, 0, b);//這個b什么意思?寫b個長度的字節嗎?但是沒有給b賦值啊
out.flush(); //通過將所有已緩沖輸出寫入底層流來刷新此流
}
in.close();
out.close();
}
2017-01-05
如果文件很小,你申請的容量8*1024個byte的數組就已經可以完全裝下,很顯然那么第一次讀就一下把文件讀完了;
如果文件很大,你申請的容量一次讀取裝不完時,那就會有下一次讀取,直到讀到文件的末尾,此時返回-1,就跳出循環了;
read(buf,0,buf.length)意思是一次最多讀取的字節數不超過8*1024,即<=8*1024.
看一下這個方法的官方文檔解釋,你就會更加明白了.
?????*Reads up to <code>len</code> bytes of data from this input stream
? ? ?* into an array of bytes. If <code>len</code> is not zero, the method
? ? ?* blocks until some input is available; otherwise, no
? ? ?* bytes are read and <code>0</code> is returned.
2017-01-05
b=in.read(buf,0,buf.length)就是在給b賦值,返回一個int 型的數,意思是,read方法從流中一次所讀到的字節數.