![](/img/trans.png)
[英]Folder and file are not getting created by Java code under Linux, but it works for Windows
[英]java code to search a file entire system, works fine on windows but infinite in linux ubuntu
開發搜索實用程序來搜索整個計算機系統的文件,在Windows平台上運行良好,但在ubuntu linux中成為一個無限的過程。 請幫助克服這個缺陷。 以下是代碼的主要部分。
public static void fun(File f){ // root directory is passed as argument
try{
if(f.isDirectory()){
File [] fi=f.listFiles();
for(int i=0;i<fi.length;i++){
if(fileFound==true) break; // fileFound is boolean data type used as flag to indicate whether the file is found or not
System.out.println(fi[i].getName());
fun(fi[i]);
}
}
else{
if(f.getName().equalsIgnoreCase(txtFile.getText()) ||
(f.getName().toLowerCase().startsWith(txtFile.getText().toLowerCase())) ||
(f.getName().toLowerCase().endsWith(txtFile.getText().toLowerCase()))){
l.setText("file found " + f.getAbsolutePath()); // l is JLabel that indicated prints the info like file found and its path
fileFound=true;
}
}
}
catch(Exception e){
}
}
您觀察到的錯誤可能是由嵌套的符號鏈接引起的 。
解決此問題的最有效方法是使用優秀的Apache Commons IO庫中的FileUtils#iterateFiles
。
有類似“。”的東西。 (當前目錄)和linux中每個目錄中的“..”(上面的目錄)。 也許這就是你的問題。
在unix like系統中,第一個文件夾是“。” (當前文件夾),第二個文件夾是“..”(根文件夾)
你應該跳過前2個文件夾,以避免一遍又一遍地訪問同一個文件夾。
嘗試:
if(fi[i].getName() == "." || fi[i].getName() == "..")
continue;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.