package xxx.yyy.zzz;
import java.io.*;
/**
* 以位元組流讀取檔案和寫入檔案
*/
public class IOUtil {
//檔案拷貝,批量讀取
public static void copyFile(File srcFile, File destFile) throws IOException{
if (!srcFile.exists()){
throw new IllegalArgumentException("檔案:" + srcFile + "不存在");
}
if (!srcFile.isFile()){
throw new IllegalArgumentException((srcFile + "不是檔案"));
}
FileInputStream in = new FileInputStream((srcFile));
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);
out.flush();
}
in.close();
out.close();
}
//帶緩存的拷貝
public static void copyByBuffer(File srcFile, File destFile) throws IOException{
if (!srcFile.exists()){
throw new IllegalArgumentException("檔案:" + srcFile + "不存在");
}
if (!srcFile.isFile()){
throw new IllegalArgumentException((srcFile + "不是檔案"));
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int c;
while ((c = bis.read()) != -1){
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
//單位元組拷貝
public static void copyByByte(File srcFile, File destFile) throws IOException{
if (!srcFile.exists()){
throw new IllegalArgumentException("檔案:" + srcFile + "不存在");
}
if (!srcFile.isFile()){
throw new IllegalArgumentException((srcFile + "不是檔案"));
}
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int c;
while ((c = fis.read()) != -1){
fos.write(c);
fos.flush();
}
fis.close();
fos.close();
}
}