gpt4 book ai didi

java - NoSuchFileException 使用 File.copy

转载 作者:行者123 更新时间:2023-11-29 06:26:10 26 4
gpt4 key购买 nike

尝试将图像文件从一个文件夹复制到另一个文件夹时出现以下错误。

错误信息:

java.nio.file.NoSuchFileException: C:\Users\William\Pictures\D8McXhNVUAE7VFh.jpg -> \resources\6.jpg
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.copy(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source)
at java.nio.file.Files.copy(Unknown Source)
at data.DAO.addEmployee(DAO.java:130)
at GUI.NewUser$3.actionPerformed(NewUser.java:184)
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)

我的代码为:

String sourceFile = "C:\Users\xxxxxx\Pictures\D8McXhNVUAE7VFh.jpg"
String destinationFilePath = "\\resources\\" + generatedID + ".jpg" ;
File sourceFile = new File(imageSourcePath);
File destinationFile = new File(destinationFilePath);
Path sourcePath = sourceFile.toPath();
Path destinationPath = destinationFile.toPath();
try {
Files.copy(sourcePath, destinationPath);
} catch (IOException ex) {
ex.printStackTrace();
}

这个问题的原因可能是什么?

最佳答案

您在 String sourceFileFile sourceFile 中使用了相同的变量名。请重命名其中一个。

举个例子,用这个

ex -: String sourceFile
File file

关于java - NoSuchFileException 使用 File.copy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59055803/

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