out.write(buf, 0, b);//這個(gè)b什么意思?寫b個(gè)長度的字節(jié)嗎?但是沒有給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);//這個(gè)b什么意思?寫b個(gè)長度的字節(jié)嗎?但是沒有給b賦值啊
out.flush(); //通過將所有已緩沖輸出寫入底層流來刷新此流
}
in.close();
out.close();
}
2017-01-05
如果文件很小,你申請(qǐng)的容量8*1024個(gè)byte的數(shù)組就已經(jīng)可以完全裝下,很顯然那么第一次讀就一下把文件讀完了;
如果文件很大,你申請(qǐng)的容量一次讀取裝不完時(shí),那就會(huì)有下一次讀取,直到讀到文件的末尾,此時(shí)返回-1,就跳出循環(huán)了;
read(buf,0,buf.length)意思是一次最多讀取的字節(jié)數(shù)不超過8*1024,即<=8*1024.
看一下這個(gè)方法的官方文檔解釋,你就會(huì)更加明白了.
?????*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賦值,返回一個(gè)int 型的數(shù),意思是,read方法從流中一次所讀到的字節(jié)數(shù).