1. 程式人生 > >java 複製檔案和圖片

java 複製檔案和圖片

一 複製檔案採用 BufferedReader和 BufferedWriter 作為快取


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br=
                 new BufferedReader(new FileReader("c:\\2.txt"));
String b = null;
//寫入檔案
BufferedWriter bw =
                 new BufferedWriter(new FileWriter("c:\\c.txt")); 
//每次讀一行 對於讀取 txt或者html 等有換行符是可以的  while ((b = br.readLine()) != null) { 
                 bw.write(b);//輸出字串 
bw.newLine();
//將快取寫入檔案
bw.flush(); 


br.close();
bw.close();

}
二 複製圖片

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream("c:\\a.jpg");
        FileOutputStream fos = new FileOutputStream("c:\\b.jpg");

byte[] buffer = new byte[2048];
         int len =0;
while((len=fis.read(buffer))>0){
fos.write(buffer, 0, len);
}

fis.close();
fos.close();
}