gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:52:42 24 4
gpt4 key购买 nike

我正在尝试实现一个功能,可以从我的源目录获取文件准备包目录以迁移到服务器中。这是一个使用目标和目的地将所有 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)

the exception line is highlight as sourceChannel.close();

最佳答案

sourceChannel.close(); 行出现 NullPointerException。

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

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

FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

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

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 IOFileUtils.copyFile方法:

FileUtils.copy(source, dest);

关于java - 无法将包的源从目标复制到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52033795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com