简体   繁体   English

java代码搜索整个系统的文件,在windows上工作正常但在linux ubuntu中无限

[英]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.

相关问题 在Linux下,不会通过Java代码创建文件夹和文件,但适用于Windows - Folder and file are not getting created by Java code under Linux, but it works for Windows Java 代码在 Windows 中有效,但在 Linux(Fedora 或 Ubuntu)中无效 - Java code worked in Windows but not in Linux (Fedora or Ubuntu) Linux上的Java SSL连接重置,Windows工作正常 - Java SSL connection reset on Linux, Windows works fine java 命令在 linux 中抛出错误,但在 windows 中运行良好 - java command throwing an error in linux although works fine in windows 我的java代码在linux中正常工作但在Windows上不能正常工作 - My java code works properly in linux but not on Windows 代码在JAVA上可以正常运行,但无法在Android上创建解密文件 - Code works fine on JAVA But Fails to Create Decrypted File on Android Java无法在Windows上的ubuntu机器上读取csv文件? - Java Unable to read csv file on ubuntu machine works on windows? Java URLConnection适用于Windows,但不适用于Linux - Java URLConnection works with windows,but not with linux 如何在 Java 中使用操作系统的默认文件选择器(在 Linux 和 Windows 中) - How to use the default File Chooser for the operating system in Java (in Linux and Windows) Java:Linux上的字体看起来很乱,但在Windows上很好 - Java: Fonts look garbled on Linux, but fine on windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM