简体   繁体   English

复制文件期间可能发生什么异常?

[英]What are the possible exception occurs during copying file?

My program has to copy file from one folder to another folder. 我的程序必须将文件从一个文件夹复制到另一个文件夹。 I have used InputStream and OutputStream to do he same. 我已经使用InputStream和OutputStream来做同样的事情。 The file size is about 5GB. 文件大小约为5GB。 what are all the possible exception may occur during this process and how?. 在此过程中可能发生的所有异常情况有哪些?如何发生? As i need mention the same in unit testcase document...Please help. 因为我需要在单元测试用例文档中提到相同的内容...请帮助。

Look at the java docs for the methods you are using. 查看您使用的方法的Java文档 Any Sun Java docs will mention exceptions that can be thrown by methods. 任何Sun Java文档都会提到方法可以抛出的异常。

I am guessing FileNotFoundException, NullPointerException, IOException. 我猜FileNotFoundException,NullPointerException,IOException。

Also consider using NIO for file copying, as it is likely that you'll get better performance which should be noticable with such a large file: 还可以考虑使用NIO进行文件复制,因为这样可能会获得更好的性能,这对于如此大的文件应该是显而易见的:

Take a look at this post from JavaLobby, which shows a static file copy method using java.io.FileChannel s to do the grunt work. 看一下JavaLobby上的这篇文章 ,它显示了使用java.io.FileChannel进行静态工作的静态文件复制方法。

As a general rule, checked exceptions don't pop out of thin air. 通常,检查异常不会突然冒出来。 They are explicitly declared to be thrown by the methods you use (check the Java API ). 它们被明确声明为由您使用的方法抛出(请检查Java API )。 A good IDE (eg NetBeans ) would even automatically prompt you to catch or declare these exceptions whenever you encounter them. 一个好的IDE(例如NetBeans )甚至会在遇到它们时自动提示您捕获或声明这些异常。 Besides, you wouldn't even be able to compile your code if you didn't handle these exceptions. 此外,如果不处理这些异常,您甚至将无法编译代码。 You have been documenting all these exceptions with Javadoc right? 您已经用Javadoc记录了所有这些异常,对吗? =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM