簡體   English   中英

Java在特定文件夾中創建文件

[英]java create file in specific folder

我正在嘗試在特定文件夾中創建一個臨時文件。在下圖中,您可以找到我項目的結構樹。 我目前在FileAnalyse.java中,並嘗試在webcontent下的數據中創建文件。 以下嘗試對我不起作用:

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

要么

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

這對我有用

注意:

您的版本:

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

正確版本:

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

編輯:

當您打印出當前正在使用的路徑時,可以嘗試一下檢查路徑是否正確(或者您是否在正確的軌道上以獲取正確的路徑):

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

這樣,您可以檢查絕對路徑,然后可以查看當前嘗試的Pathstring是否幾乎正確

我通過運行配置-> tomcat->更改工作目錄來更改eclipse的工作目錄來解決了這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM