import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo3 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { copyImage(); } public static void copyImage() throws IOException { // 找到目標文件 File file = new File("C:\\cc\\kk\\1.jpg"); // 創建通道 FileInputStream inputStream = new FileInputStream(file);
File file1 = new File("C:\\cc\\tc.jpg"); FileOutputStream outputStream = new FileOutputStream(file1);
//建立一個緩衝區 byte[] b = new byte[1024 * 6]; //循環遍歷文件內容,沒有就返回-1 while (inputStream.read(b) != -1) {
//寫入數據 outputStream.write(b); }
//關閉流 關閉流的原則:先開後關,後開先關 outputStream.close(); inputStream.close(); } }