首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用File.copy的NoSuchFileException

使用File.copy的NoSuchFileException
EN

Stack Overflow用户
提问于 2019-11-27 00:53:02
回答 3查看 90关注 0票数 1

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

错误消息:

代码语言:javascript
复制
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)

我的代码如下:

代码语言:javascript
复制
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();
}

此问题的原因可能是什么?

EN

回答 3

Stack Overflow用户

发布于 2019-11-27 01:20:47

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

作为示例,请使用以下内容

代码语言:javascript
复制
ex -: String sourceFile
      File file
票数 3
EN

Stack Overflow用户

发布于 2019-11-27 01:00:07

您需要在路径字符串中使用双反斜杠,例如C:\\Users\\xxxxxx\\Pictures\\D8McXhNVUAE7VFh.jpg

票数 2
EN

Stack Overflow用户

发布于 2019-11-27 01:00:54

当您使用Files.copy处理图像时,您应该通过流进行复制。

代码语言:javascript
复制
URI u = URI.create("file:///C:\Users\xxxxxx\Pictures\D8McXhNVUAE7VFh.jpg");
 try (InputStream in = u.toURL().openStream()) {
     Files.copy(in, path);
 }

添加是否需要通过Inout流复制:

How to make an exact copy of image in java?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59055803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档