Java文件复制和删除

时间:2022-03-28 16:26:08

文件复制

public static void fileCopy() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("D:" + File.separator + "001.flv");
fos = new FileOutputStream("D:" + File.separator + "002.flv");
byte[] b = new byte[1024 * 10];
int len = -1;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
System.out.println("复制完毕");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null && fos != null) {
fis.close();
fos.close();
}
} catch (Exception e) {
}
}
}
文件删除

/**
* 删除文件夹或文件
*
* @param file
* @return
*/
public static boolean fileDelete(File file) {
if (!file.exists())
return false;
if (file.isFile()) {
return file.delete();
}
File[] childFiles = file.listFiles();
for (File f : childFiles) {
fileDelete(f);
}
return file.delete();
}