本文实现文件对拷,从文件1拷贝到文件2,若文件2存在则会被覆盖;适用于任何文件
参数说明:F1:源文件路径+文件名
F2:目标文件+文件名
public void saveFileToFile(String F1, String F2) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(F1)); // 建立文件输入流
fos = new FileOutputStream(F2);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException ex) {
System.out.println("Source File not found:" + F1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (fis != null)
fis.close(); // 一定要进行文件的关闭,否则在新文件会是空的!
if (fos != null)
fos.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}
参数说明:F1:源文件路径+文件名
F2:目标文件+文件名
public void saveFileToFile(String F1, String F2) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File(F1)); // 建立文件输入流
fos = new FileOutputStream(F2);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException ex) {
System.out.println("Source File not found:" + F1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
try {
if (fis != null)
fis.close(); // 一定要进行文件的关闭,否则在新文件会是空的!
if (fos != null)
fos.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
}