[英]How to create a file in a eclipse project directory without knowing the exact path?
[英]How to access an existing directory in your project without knowing it's path Java
我想寫一個遍歷名為“ Users”的目錄中當前文件的方法。 由於不同的機器具有不同的絕對路徑,因此我需要一種無需對該路徑進行硬編碼即可訪問此文件夾的方法。
假設我在src文件夾中,並且Users文件夾已經存在。
public static boolean validUsername(String user) {
File dir = new File("./Users/");
File[] directoryListing = dir.listFiles();
System.out.println(dir);
System.out.println(directoryListing);
if (directoryListing != null) {
for (File child : directoryListing) {
// Do something with child
// think child is filename?
if (user.equals(child.getName())){
return false;
}
}
}
return true;
}
每當我在main方法中調用此方法時,即使Users文件夾中已經有文件,目錄列表變量始終為null。
有很多解決方法。 有一些
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.