Question

i want to search for a file in windows just by giving the name of the file,intially i tried to list out all files using this piece of code

File[] files = File.listRoots();   
for(File f : files){   
  parseAllFiles(f.getPath());   
}   


...

public static void parseAllFiles(String parentDirectory){   
  File[] filesInDirectory = new File(parentDirectory).listFiles();   
  for(File f : filesInDirectory){   
    if(f.isDirectory()){   
      parseAllFiles(f.getAbsolutePath());   
    }   
    System.out.println("Current File -> " + f);             
  }     
}

but I got an exception saying

Exception in thread "main" java.lang.NullPointerException
    at fileoper.parseAllFiles(fileoper.java:24)
    at fileoper.parseAllFiles(fileoper.java:26)
    at fileoper.parseAllFiles(fileoper.java:26)
    at fileoper.main(fileoper.java:19)

Any suggestions on this?

Was it helpful?

Solution 2

I tried your example. I get a null pointer when trying to open /root. When I try to open it via terminal, it just says that I don't have the permissions.

So you basically have a problem with permissions.

The javadoc says :

̀Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

In order to catch this, you should check if filesInDirectory is null.

public static void parseAllFiles(String parentDirectory){   
  File[] filesInDirectory = new File(parentDirectory).listFiles();
  //Added this line.
  if(filesInDirectory != null){   
    for(File f : filesInDirectory){   
      if(f.isDirectory()){   
        parseAllFiles(f.getAbsolutePath());   
      }   
      System.out.println("Current File -> " + f);             
    }
  }
}

OTHER TIPS

You can use the the FileVisitor in Java and then Files.walkFileTree(startDir, visitor) to walk the filetree recursively from a starting dir.

FileVisitor defines a couple of methods which are called for every File/Directory (see here). Of particular interest is the method

FileVisitResult visitFile(T file, BasicFileAttributes attrs)

which is called for every File. In this method you can check if it is the file you are searching for.

(Use SimpleFileVisitor and override just the visitFile() method if you don't need the other methods)

See Walking the File Tree for a complete description of how to use it.

You are calling listFiles() method directly on file object.It returns null if file is not a directory,that's why you are getting NPE.So verify first file is dierctory or not and then call the method.
According to File#listFiles()

pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned

This is the code to modify

public static void parseAllFiles(String parentDirectory)
{        
          File file =  new File(parentDirectory);
          if(file.isDirectory())
         {
           File[] filesInDirectory =listFiles();   
           for(File f : filesInDirectory)
           {    
                    parseAllFiles(f.getAbsolutePath());   
            }   

          }else{
               System.out.println("Current File -> " + file);                  
          }

}

You can check in parseAllFiles() if all files you want to open actually exist by:

File fileInDirectory = new File(parentDirectory);
  if(fileInDirectory.exists()){
  File[] filesInDirectory = fileInDirectory.listFiles();
} 

Then you can realize what file has not been found/created.

The same you can do in the first loop:

for(File f : files){  
  if(f.exists()) 
  parseAllFiles(f.getPath());   
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top