在尝试将图像文件从一个文件夹复制到另一个文件夹时,出现以下错误。
错误消息:
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();
}此问题的原因可能是什么?
发布于 2019-11-27 01:20:47
您在String sourceFile和File sourceFile中使用了相同的变量名称。请将重命名为其中之一。
作为示例,请使用以下内容
ex -: String sourceFile
File file发布于 2019-11-27 01:00:07
您需要在路径字符串中使用双反斜杠,例如C:\\Users\\xxxxxx\\Pictures\\D8McXhNVUAE7VFh.jpg
发布于 2019-11-27 01:00:54
当您使用Files.copy处理图像时,您应该通过流进行复制。
URI u = URI.create("file:///C:\Users\xxxxxx\Pictures\D8McXhNVUAE7VFh.jpg");
try (InputStream in = u.toURL().openStream()) {
Files.copy(in, path);
}添加是否需要通过Inout流复制:
https://stackoverflow.com/questions/59055803
复制相似问题