0

I put a file in a folder named "templates" placed in the src/ of my eclipse project. I'm trying to make a copy of that file, and want to edit it. Here's my code:

InputStream source = getClass().getClassLoader().getResourceAsStream("templates/file.docx");
File dest = new File("templates/updatedfile.docx");

try{
    Files.copy(source, dest.toPath());
}
catch(Exception e)
{
    e.printStackTrace();
}

However, I'm getting a java.nio.file.NoSuchFileException: template/updatedfile.docx! What have I done wrong?

Edit: Sorry, fixed a typo in the code, exact exception is now updated

  • Seems that this file *templates/file.docx* is not being recognized as a resource in your application. Make sure this file is inside your sources. Otherwise, I would recommend using the concrete path instead. – Luiggi Mendoza Aug 11 '14 at 19:36
  • @BackSlash that's a generic Q/A for `NullPointerException`. This case is a rather specific problem, IMO the API should throw an `IOException` for not finding the file at `getClass().getClassLoader().getResourceAsStream`. – Luiggi Mendoza Aug 11 '14 at 19:37
  • Where exactly do you get the exception? – jny Aug 11 '14 at 19:43
  • @jny after I made the changes as suggested in the answer, the exception is back to nullpointerexception! – user3930896 Aug 11 '14 at 19:52

2 Answers2

0

You can use:

System.out.println(System.getProperty("user.dir"));

to see where is the root folder java looks at by default, which is probably your "Project folder". the /src folder is under the project folder so you probably have to add that to the path too:

"src/templates/updatedfile.docx"

gkrls
  • 2,618
  • 2
  • 15
  • 29
0

When you run an application in eclipse it runs in your project folder. So when you create a new file templates/updatedfile.docx, you are trying to create a new directory and file at the same time. Try creatign templates directory in your project folder and run the app again. Alternatively in your code you can check if the directory exists, and create it if it does not.

jny
  • 8,007
  • 3
  • 37
  • 56
  • Sorry if I wasn't clear, I already have the templates/ directory - just need to copy the file there! – user3930896 Aug 11 '14 at 20:13
  • Well, it should not be in your `src` directory. Just create templates directory in the project root directory and put your source file there. You would need to change the code for the source file path. – jny Aug 11 '14 at 20:15
  • But if you want to keep things the way they are now, you need to change the path for dest file to `bin/templates/...`. That is where your app is reading the source file from. – jny Aug 11 '14 at 20:18
  • I've tried keeping everything in the root folder to use relative path for input (templates/file.docx) and output(templates/updatedfile.docx) but it didn't work! – user3930896 Aug 11 '14 at 20:22
  • I'm getting a java.nio.file.nosuchfileexception template/file.docx – user3930896 Aug 11 '14 at 20:26
  • The exception you posted say `template`, not `templates`.... Is it typo in the code or comment? – jny Aug 11 '14 at 20:28