I cannot figure out what I am doing wrong, I'm just going off what the task gave me and all it was was the function syntax and parameters, so I'm going off what I've learned. If possible, I would like an explanation for the solution, so I can resolve other things, thanks!
What am I doing wrong with the function?
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
13 May, 14:10
The Files.copy() method you are using specifies one path to copy as the first argument and the second argument is the full path & file name of what the copy will be saved as. For example, say the first argument is the full path to a file and the second argument is the full path to a directory (as this code does), the result will be that the file is then saved as a directory. Of course, if (and when) that directory exists, an exception will be thrown saying that the directory already exists. This needs to be fixed in the above code to pass.
0
haayden
13 May, 15:43
So you're saying I also need to find a random file in the targetDirectory to set as my second argument?
+1
Guadalupe Gagnon
13 May, 15:58solution
no. My assumption is to just use the file name in the source as the new file name in the desination.
So the source will be the directory with files in it. The destination will (most likely) be an empty empty. For each file in source get the file name and then add it to the destination directory.
So if the source has 3 files in it named file1.txt, file2.txt, and file3.txt your code should do this:
sourcePath/file1.txt → destinationPath + file1.txt
sourcePath/file2.txt → destinationPath + file2.txt
sourcePath/file3.txt → destinationPath + file3.txt
p.s. don't forget about the forward slash(es) / that are in file paths
+1
haayden
13 May, 16:18
i'll try that out
+1
haayden
13 May, 16:23
something like this?
+1
Guadalupe Gagnon
13 May, 16:36solution
I don't think that will work because you need the double slashes in there:
**However**
Someone (Lisa) told me of a much more elegant solution. Paths have a resolve method. You use that with a Path that is a directory and pass it a fileName and it will return a new path that is the directory plus the filename formatted properly for use:
So lets say you have a source Path that is a file, such as:
C:/user/desktop/myFile.txt
and a destination directory such as:
C:/user/desktop/target
your would do this:
This returns a path that is:
C:/user/desktop/target/myFile.txt
+2
haayden
13 May, 16:39
i can sleep in peace now, thanks so much
+1