NoSuchFileException using File.copy

I got the following error when trying to copy an image file from one folder to another.

Error message :

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(
    at GUI.NewUser$3.actionPerformed(
    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)

My code as:

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

What could be the cause of this problem?

You have used same variable name in String sourceFile and File sourceFile. Please rename one of them.

for an example use this

ex -: String sourceFile
      File file

Copy a file in Java, To copy a file to a target file, we can use its copy(Path source, Path target, CopyOption options) method. This method takes NoSuchFileException is thrown. Sign-Up for JavaBrahmanWeekly! All New Subscribers will get a free e-book on Lambda Expressions in Java-8!. Get regular stream of articles in Java, J2EE & Design Patterns.

You need to use double backslashes in your path string, e.g. C:\\Users\\xxxxxx\\Pictures\\D8McXhNVUAE7VFh.jpg

Java NIO - Copy File or Directory Examples, Use the enums StandardCopyOption and LinkOption for this parameter with the following values: COPY_ATTRIBUTES: copy attributes to the new� Best Java code snippets using java.nio.file.NoSuchFileException (Showing top 20 results out of 900) file); } // Copy attributes from original file return new Path

When you are using Files.copy for images you should copy by stream .

URI u = URI.create("file:///C:\Users\xxxxxx\Pictures\D8McXhNVUAE7VFh.jpg");
 try (InputStream in = u.toURL().openStream()) {
     Files.copy(in, path);

Adding If you need to copy by Inout stream :

How to make an exact copy of image in java?

3 ways to Copy a File From One Directory to Another in Java, It was easy to write your own routine to copy a file using FileInputStream or FileChannel, most NoSuchFileException: programming.txt -> targetrr\java7.txt File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), True) Next ' Copy text files. For Each f As String In txtList 'Remove path from the file name. Dim fName As String = f.Substring(sourceDir.Length + 1) Try ' Will not overwrite if the destination file already exists.

Java IO & NIO - Files.copy() Examples, public static Path copy(Path source, Path target, CopyOption options) throws IOException. This method copies a file to the target file. Parameters: source - the � Above assume that target folder already exists, if “target” directory does not exist, the system will throw NoSuchFileException. Most of the time we want to Copy files from one directory to another including sub directories. We will use Files.walkFileTree from NIO Java7 approach to recursively copy directory and all files to another location.

java.nio.file.Files, Parameters: path - the path to the file to delete; Throws: NoSuchFileException - if the file This method copies a file to the target file with the options parameter� This is the conventional way of file copy in java. Here we create two Files – source and destination. Then we create InputStream from source and write it to the destination file using OutputStream for java copy file operation. Here is the method that can be used for java copy file using streams.

Moving a File or Directory (The Java™ Tutorials > Essential Classes , The following shows how to use the move method: import static java.nio.file. StandardCopyOption.*; Files.move(source, target, REPLACE_EXISTING);. Though� The following examples show how to use java.nio.file.CopyOption. These examples are extracted from open source projects. These examples are extracted from open source projects. Example 1

  • Either the source file doesn't exist or the destination file path doesn't exist. I don't program on Windows, but that path looks fishy to me.
  • That would cause a compilation error. They would not be able to run their program. It's pretty safe to assume that what they have there is just an example input.