JavaSeIO流小练习——文件复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) {
copyFile();
}
public static void copyFile() {
File oldFile = new File("E:/123.txt");
File newFile = new File("D:/1.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(oldFile);
fos = new FileOutputStream(newFile);
byte[] bytes = new byte[1024];
while (true) {
int len = fis.read(bytes, 0, bytes.length);
if (len == -1) {
break;
} else {
fos.write(bytes, 0, len);
}
}
System.out.println("文件复制成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果: