[英]java code to search a file entire system, works fine on windows but infinite in linux ubuntu
Developing search utility to search a file entire computer system, works fine on windows platform but becomes an infinite process in ubuntu linux. 开发搜索实用程序来搜索整个计算机系统的文件,在Windows平台上运行良好,但在ubuntu linux中成为一个无限的过程。 Please help to overcome this flaw. 请帮助克服这个缺陷。 The following is the main part of the code. 以下是代码的主要部分。
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){
}
}
The error you are observing may be due to nested symbolic links . 您观察到的错误可能是由嵌套的符号链接引起的 。
The most effective approach to solve this problem would be to instead use FileUtils#iterateFiles
from the excellent Apache Commons IO library. 解决此问题的最有效方法是使用优秀的Apache Commons IO库中的FileUtils#iterateFiles
。
There is something like "." 有类似“。”的东西。 (current directory) and ".." (above directory) in each dir in linux. (当前目录)和linux中每个目录中的“..”(上面的目录)。 Maybe thats your problem. 也许这就是你的问题。
In unix like systems the first folder is "." 在unix like系统中,第一个文件夹是“。” (current folder) and the second folder is ".." (the root folder) (当前文件夹),第二个文件夹是“..”(根文件夹)
you should skip the first 2 folders to avoid getting to the same folder over and over again. 你应该跳过前2个文件夹,以避免一遍又一遍地访问同一个文件夹。
try: 尝试:
if(fi[i].getName() == "." || fi[i].getName() == "..")
continue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.