利用IO實現文件複製

import java.io.*;

/**
 * 利用IO實現文件複製
 */
public class FileCopy {

    public static void main(String[] args) {
        File source = new File("D:\\source.jpg");
        File target = new File("D:\\target.jpg");
        try(
                FileInputStream fileInputStream = new FileInputStream(source);
                FileOutputStream fileOutputStream = new FileOutputStream(target);
        ) {
            byte[] buffer = new byte[1024];
            int byteRead;
            while ((byteRead = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, byteRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

發佈了334 篇原創文章 · 獲贊 66 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章