0

I know how to create file in Java with a specific path.
I want to create file in the source folder of the project without specifing the drive because it can change from PC to PC.

I tried to use:

File targetFile = new File("/src/SavedGames/uploadedFile.xml");
targetFile.createNewFile();

with dot before the 'src':

File targetFile = new File("./src/SavedGames/uploadedFile.xml");
targetFile.createNewFile();

with \\:

File targetFile = new File("\\src\\SavedGames\\uploadedFile.xml");
targetFile.createNewFile();

It doesn't work, it throws exception.

This one works but creates it on my Apache server folder:

File targetFile = new File("uploadedFile.xml");
targetFile.createNewFile();

This is the hierarchy:
enter image description here

The code runs on the LoadGameServlet.java

Community
  • 1
  • 1
E235
  • 11,560
  • 24
  • 91
  • 141
  • Have you tried `src/SavedGames/uploadedFile.xml`? – gtgaxiola Oct 01 '14 at 19:35
  • checked now: it tries to create is on c:\myApacheServer\bin\src\SavedGames – E235 Oct 01 '14 at 19:39
  • If `src/SavedGames/iploadedFile.xml` doesn't work, check your `Class-Path` in mainfest.mf to make sure it leads to the root of your project. – Santiago Benoit Oct 01 '14 at 19:41
  • See http://stackoverflow.com/questions/18530112/how-to-create-files-under-web-inf – Sualeh Fatehi Oct 01 '14 at 19:41
  • @SualehFatehi, the solution didn't work for me. I tried the solution: URL path = Thread.currentThread().getContextClassLoader().getResource("src/SavedGames/"); but it still creates it on apache server – E235 Oct 01 '14 at 19:46

2 Answers2

0

You can use something like System.out.println(targetFile.getAbsolutePath()) to debug where the file would be created, even before creating the actual file.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • Good idea, it creates it on C:\src\SavedGames - there is no such directory. – E235 Oct 01 '14 at 19:38
  • You should then create the folder first, or use the `.mkdirs()` function to create it programmatically. – Mike Laren Oct 01 '14 at 19:41
  • But I don't want to create it on 'C:' drive because maybe the user already has such folder. I watned to create it on my server SavedGames package. – E235 Oct 01 '14 at 19:43
  • Your current folder at runtime seems to be the Apache folder, so all relative paths such as "src/" or "./src/" are relative to that folder. You should make sure that you use the right combination of ".." and relative folders to point the file to the location that you want. – Mike Laren Oct 01 '14 at 19:52
  • Also, if you use ".." in your folder names, you can also use the method `targetFile.getCanonicalPath()` to resolve the exact location where the file will be stored to. – Mike Laren Oct 01 '14 at 19:53
0

After searching alot I found a way to do it.

File targetFile = new File(new File(LoadGameServlet.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent() + "\\uploadedFile.xml");
String decoderPath = java.net.URLDecoder.decode(targetFile.getPath(), "UTF-8");
String newPath = decoderPath .replaceAll("build\\\\web\\\\WEB-INF\\\\classes\\\\Servlets", "src\\\\java\\\\SavedGames");
targetFile = new File(newPath);
targetFile.createNewFile();

I checked it and it works fine.

Thanks for your help.

E235
  • 11,560
  • 24
  • 91
  • 141