mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-17 04:51:58 -05:00
update:demo5
This commit is contained in:
20
com/study/demo5/FileCopy.java
Normal file
20
com/study/demo5/FileCopy.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.study.demo5;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class FileCopy {
|
||||
public void copy(String source, String target) {
|
||||
try (FileInputStream fis = new FileInputStream(source);
|
||||
FileOutputStream fos = new FileOutputStream(target)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println("源文件未找到: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
System.err.println("文件复制失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user