0

I am trying to create a temp file in a specific folder In the image below, you can find the structure tree of my project. I am currently in FileAnalyse.java and trying to create the file in data, under webcontent. The following tries did not work for me:

File dir = new File(System.getProperty("user.dir")+"/WebContent/data");
File subjectFile = File.createTempFile("subjects", ".json",dir); 

or

File dir = new File("/WebContent/data");
File subjectFile = File.createTempFile("subjects", ".json",dir); 


Project layout image

6
  • File dir = new File("../WebContent/data"); I'm not sure if this works, but usually with .. you can get one step back in the folder hierarchy. So because of your packages it could be File dir = new File("../../../../WebContent/data"); //src/com/packages/rdf need to be gone back Commented Mar 26, 2015 at 13:42
  • thank you, but it didn't work Commented Mar 26, 2015 at 13:44
  • What exactly do you mean by "it didn't work"? Can you post an MCVE? Commented Mar 26, 2015 at 13:46
  • java.io.IOException: No such file or directory Commented Mar 26, 2015 at 13:46
  • I can't post the entire method, it is too long for comment and rest of the code is irrelevant anyways, normally I can create a temporary file but it doesn't work when I give it a directory parameter. Commented Mar 26, 2015 at 13:48

2 Answers 2

2
File dir = new File("WebContent/data");
System.out.println(dir.getAbsolutePath()); //check the path with System.out
File subjectFile = File.createTempFile("subjects", ".json",dir); 

This worked for me

note:

Your Version:

File dir = new File("/WebContent/data"); //has a / before WebContent

Correct Version:

File dir = new File("WebContent/data"); // no / before WebContent

Edit:

You can check if your path is the correct one with your attempts (or if you're on the right track to get the correct) when you print out the Path you're currently working with:

File dir = new File("YOUR/ATTEMPT");
System.out.println(dir.getAbsolutePath()); //check the path with System.out

that way you can check the absolute path, and you can look if the current attempted Pathstring is nearly correct

Sign up to request clarification or add additional context in comments.

7 Comments

didn't work for me, maybe I should specify that I am using osx
Hmm I'm not sure if there are differences, but I'm using Windows
Can you add the System.out.println(dir.getAbsolutePath()); after File dir... Line, and give the output for your different attempts?
sure, before doing that: File dir = new File("WebContent/data"); System.out.println("I am here 1"); File subjectFile = File.createTempFile("subjects", ".json",dir); System.out.println("I am here 2"); it prints I am here 1 but not 2
here you go /Users/emrahozkan/Documents/programs/eclipse/Eclipse.app/Contents/MacOS/WebContent/data
|
0

I solved the problem by changing the working directory of eclipse by going to run configurations->tomcat->change working directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.