简体   繁体   中英

java create file in specific folder

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); 


项目布局图

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

我通过运行配置-> tomcat->更改工作目录来更改eclipse的工作目录来解决了这个问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM