无法将源从包的目标复制到目标

  • 本文关键字:目标 复制 java file-io
  • 更新时间 :
  • 英文 :


我正在尝试实现一个函数,该函数可以从我的源目录获取到文件准备包目录以迁移到服务器。这是一个使用目标和目标将所有 java 文件复制到相应文件中的函数,如果我在包文件夹中声明。

private static void copyfilesforsurce(File source, File dest) throws IOException { 
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{

sourceChannel.close((;

destChannel.close();
}}

但我得到以下异常:

at preparepackage.preparepackagefolder.copyFileUsingJava7Files(preparepackagefolder.java:82)
at preparepackage.preparepackagefolder.access$14(preparepackagefolder.java:74)
at preparepackage.preparepackagefolder$3.actionPerformed(preparepackagefolder.java:233)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

异常行突出显示为 sourceChannel.close((;

你在行sourceChannel.close();有一个 NullPointerException。

这意味着行sourceChannel = new FileInputStream(source).getChannel();未成功完成。

如果new FileInputStream(source)抛出一个FileNotFoundException,则sourceChannel = new FileInputStream(source).getChannel();行不会成功完成,其中FileInputStream JavaDoc说:

FileNotFoundException - 如果文件不存在,是目录而不是常规文件,或者由于某种其他原因无法打开读取。

若要验证这一点,可以在方法的开头添加以下行:

System.out.format("%s - isFile: %b, isDirectory: %b, canRead: %b", 
source, source.isFile(), source.isDirectory(), source.canRead());

此行应输出源文件的名称,后跟" - isFile: true, isDirectory: false, canRead: true"。


要将目录中的所有文件复制到其他目录中,可以使用Apache Commons IO,FileUtils.copyFile方法:

FileUtils.copy(source, dest);

最新更新