package IOProj;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyPicDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
copyPic();
// copyPic2();
}
private static void copyPic() throws IOException {
// TODO Auto-generated method stub
/**
* 複製一個圖片 1、既有讀也有寫FileInputStream和FileOutputStream 2、還是非文本資料
* 使用到了位元組流中用於操作檔案的對象
*/
//1、被複製的源檔案
FileInputStream fis = new FileInputStream("temp\\net.doc");
//2、複製之後產生的新的檔案
FileOutputStream fos = new FileOutputStream("temp\\aa.doc");
// 3、自訂緩衝區對象
byte[] buf = new byte[1024];
int by = 0;
while ((by = fis.read(buf)) != -1) {
fos.write(buf, 0, by);
}
fos.close();
fos.close();
}
public static void copyPic2() throws IOException {
FileInputStream fis = new FileInputStream("temp\\net.doc");
FileOutputStream fos = new FileOutputStream("temp\\aa.doc");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int by=0;
byte[] buf=new byte[1024];
while((by=bis.read(buf))!=-1)
{
bos.write(buf,0,by);
}
fis.close();
fos.close();
bis.close();
bos.close();
}
}