【发布时间】:2020-05-24 00:11:30
【问题描述】:
示例代码如下。它将目标文件和目录从一个位置复制到另一个位置。在跨网络处理文件时,处理 IO 异常的最佳实践是什么?
我使用了 printStackTrace(),但感觉这只是一个更好的解决方案的占位符。正在记录答案吗?除了记录之外,是否应该有另一个步骤来实际“处理”错误?
感谢您的反馈。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
This is a test program to copy a directory(s) & file(s) from one location to another.
*/
public class CopyTest{
public static void main(String[] args) {
//Declarations
String sourcePath = "I:\\MB\\PO";
String destPath = "C:\\testPO\\";
System.out.println("Source path: " + sourcePath);
System.out.println("Destination path: " + destPath);
File source = new File(sourcePath);
File dest = new File(destPath);
//Process
//Call to method copyUsingStream
long start = System.nanoTime(); //start recording how much time the copy takes.
copyUsingStream(source, dest); //method to copy the directory/files.
System.out.println("Time taken to copy the file: "+(System.nanoTime() -start) + " nanoseconds");
} //end main method
/**
The copyUsingStream method is a recursive method to copy folders and files from one location to another.
*/
private static void copyUsingStream(File source, File dest) {
if (!source.isDirectory()){
// If source is a file -> copy it to the new folder
InputStream inStream = null;
OutputStream outStream = null;
try {
inStream = new FileInputStream(source);
outStream = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
} catch(IOException ioe) {
ioe.printStackTrace();
} finally {
try{
inStream.close();
outStream.close();
System.out.println("File copied from " + source + " to " + dest + "successfully");
} catch(IOException ioe2) {
ioe2.printStackTrace();
}
}
} else {
//If a directory -> create the directory inside the new destination
//List all contents
if (!dest.exists()) {
dest.mkdir();
System.out.println("Directory copied from " + source + " to " + dest + "successfully");
}
String folder_contents[] = source.list();
for (String file : folder_contents) {
File srcFile = new File(source, file);
File destFile = new File(dest, file);
copyUsingStream(srcFile, destFile);
}
}
} //end method copyUsingStream
} //end class CopyTest
没有catch的方法:
private static void copyUsingStream(File source, File dest) throws IOException {
if (!source.isDirectory()){
// If source is a file -> copy it to the new folder
InputStream inStream = null;
OutputStream outStream = null;
try {
inStream = new FileInputStream(source);
outStream = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
} finally {
inStream.close();
outStream.close();
System.out.println("File copied from " + source + " to " + dest + "successfully");
}
} else {
//If a directory -> create the directory inside the new destination
//List all contents
if (!dest.exists()) {
dest.mkdir();
System.out.println("Directory copied from " + source + " to " + dest + "successfully");
}
String folder_contents[] = source.list();
for (String file : folder_contents) {
File srcFile = new File(source, file);
File destFile = new File(dest, file);
copyUsingStream(srcFile, destFile);
}
}
} //end method copyUsingStream
【问题讨论】:
-
您可以使用
java.nio的Files.copy,而不是使用流来复制内容,它需要两个Path对象。您可以使用Filesystems.getDefault().getPath("DirectoryNameHere", "FileNameHere");实例化Path对象 -
您好 Powerlord,感谢您的反馈。我应该澄清一下,我使用蒸汽只是为了使用它的学术目的。我确实在我的研究中看到 nio 的 file.copy 是完成此操作的更清洁和“更好”的方式。
标签: java file inputstream outputstream ioexception