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

时间:2018-08-27 06:41:19

标签: java file-io

我正在尝试实现一个功能,该功能可以从我的源目录获取到文件准备包目录,以迁移到服务器中。此功能使用目标和目标将所有java文件复制到相应的文件(如果我在package文件夹中说明的话)。

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();
   }}

但是我遇到了以下异常AS:

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();

1 个答案:

答案 0 :(得分:1)

您在sourceChannel.close();行有NullPointerException。

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

如果sourceChannel = new FileInputStream(source).getChannel();抛出new FileInputStream(source),其中FileInputStream JavaDoc说:

,则行FileNotFoundException不能成功完成。
  

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);