![](/img/trans.png)
[英]Java check if file.exists() does not work with ImageIO.read(file)
[英]File exists check does not work
這是我的代碼:
String path = "/src/cst/org/main/data/data.txt";
File f = new File( path );
if( f.exists() ) {
yesorno = true;
System.out.println( "File exists: " + yesorno );
} else {
yesorno = false;
System.out.println( "File does not exist: " + yesorno );
}
當我運行此命令時,即使該文件確實存在,我總是會得到“文件不存在...”。 如果我鍵入完整路徑(C:/ User / ....),它可以工作,但是我想通過縮短代碼來使其工作。
我知道此路徑實際上有效,因為在另一個類上,該方法找到了我的圖片(.png)。 我是否必須添加某些內容,因為它是.txt?
是的,因為您輸入的不正確,所以您需要提供文件的完整路徑。
采用 :
String path=getClass().getResource("/path/to/file").toString().replace("file:/", "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.