Reputation: 4698
i am to copy a file in scala But getting FileNotFound error, The assets folder is in the same directory where is src:
val src = new File("/assets/public/images/default/male.jpg")
val dest = new File("/assets/public/images/profile/male1.jpg")
new FileOutputStream(dest) getChannel() transferFrom(
new FileInputStream(src) getChannel, 0, Long.MaxValue )
Upvotes: 1
Views: 1849
Reputation: 2404
In your code you are trying to copy the file using FileOutputStream, which requires a valid path to the existing file else it'll throw FileNotFoundException
. (see the doc here)
val src = new File("/assets/public/images/default/male.jpg")
val dest = new File("/assets/public/images/profile/male1.jpg")
new FileOutputStream(dest) //dest should exist
Nevertheless, Play has its own utility to copy files. Here is the link.
import play.api.libs.Files
Files.copyFile(src, dest, true, true)
println(dest.getAbsolutePath()) // filepath of copied file
Also, since the files get copied to the working directory, you might not be able to see the new file in the folder structure of your favorite IDE.
Aside, you may get the path for public assets by using routes
val srcPath = routes.Assets.at("public/images/default/male.jpg").url
Upvotes: 2
Reputation: 4843
Your wording is a little ambiguous, it seems you meant to give paths relative to the current working directory. That means that this code should be executed from a directory which contains the assets
directory. If this is so, then you have made a mistake and given absolute paths to your file objects, not relative ones. All you have to do is remove the initial forward slash from those paths and it should work.
As it is, you are telling Scala/Java to look in the root directory for assets
.
Upvotes: 0
Reputation: 8932
When copying files on Java, use the FileUtils.copy(...)
from the Apache commons project.
You get "File not found" if the file cannot be found by the running process. This could be because your file is indeed not there or because the process is lacking rights to see the file.
Upvotes: 0