2017-07-21 1 views
-1
package filefinderusingglobpattern; 

import java.io.IOException; 
import java.nio.file.FileSystems; 
import java.nio.file.FileVisitResult; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.PathMatcher; 
import java.nio.file.Paths; 
import java.nio.file.SimpleFileVisitor; 
import java.nio.file.attribute.BasicFileAttributes; 
import java.util.Scanner; 

public class FileFinderUsingGlobPattern { 

    public static class Finder extends SimpleFileVisitor<Path> { 

     private FileVisitResult CONTINUE; 
     private final PathMatcher matcher; 
     private int numMatches = 0; 

     Finder(String pattern) { 
      matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); 
     } 

     // Compares the glob pattern against 
     // the file or directory name. 
     void find(Path file) { 
      Path name = file.getFileName(); 
      if (name != null && matcher.matches(name)) { 
       numMatches++; 
       System.out.println(file); 
      } 
     } 

     // Prints the total number of matches to standard out. 
     void done() { 
      System.out.println("Matched: " + numMatches); 
     } 

     // Invoke the pattern matching method on each file. 
     @Override 
     public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 
      find(file); 
      return CONTINUE; 
     } 

     // Invoke the pattern matching method on each directory. 
     @Override 
     public FileVisitResult preVisitDirectory(Path dir, 
       BasicFileAttributes attrs) { 
      find(dir); 
      return CONTINUE; 
     } 

     @Override 
     public FileVisitResult visitFileFailed(Path file, IOException exc) { 
      System.err.println(exc); 
      return CONTINUE; 
     } 
    } 

    static void usage() { 
     System.err.println("java Find <path> -name \"<glob_pattern>\""); 
     System.exit(-1); 
    } 

    public static void main(String[] args) throws IOException { 
     /* 
     if (args.length < 3 || !args[1].equals("-name")) { 
      usage(); 
     } 

     Path startingDir = Paths.get(args[0]); 
     String pattern = args[2]; 

     Finder finder = new Finder(pattern); 
     Files.walkFileTree(startingDir, finder); 
     finder.done(); 
     */ 

     Scanner sc = new Scanner(System.in); 
     //System.out.println("C:\\Users\\Ashish Jain\\Desktop\\Test_smsbroadcaster"); 

     System.out.println("Enter the <path> where you want to look for the files: "); 
     String inputPath = sc.nextLine(); 

     Path startingDir = Paths.get(inputPath); 

     if (Files.notExists(startingDir)) { 
      System.out.println(startingDir + " does not exist."); 
      return; 
     } else { 
      System.out.println(startingDir + " exists (validation successful)."); 
     } 

     System.out.println("Enter the glob pattern for the files you want to search: "); 
     String pattern = sc.nextLine(); 
     Finder finder = new Finder(pattern); 
     Files.walkFileTree(startingDir, finder); 
     finder.done(); 
    } 

} 

Ce morceau de code de recherche de fichiers Java d'Oracle lance une exception de pointeur null dans la méthode principale, ne savez pas pourquoi, s'il vous plaît, aidez-nous. Il lance une exception à la ligne 104 "Files.walkFileTree (startingDir, finder);"Ce code de recherche de fichier Java utilisant java.nio. * Package lève une exception de pointeur nul?

Exception in thread "main" java.lang.NullPointerException 
at java.util.Objects.requireNonNull(Objects.java:203) 
at java.nio.file.Files.walkFileTree(Files.java:2699) 
at java.nio.file.Files.walkFileTree(Files.java:2742) 
at filefinderusingglobpattern.FileFinderUsingGlobPattern.main(FileFinderUsingGlobPattern.java:104) 

Check this out: https://docs.oracle.com/javase/tutorial/essential/io/examples/Find.java Malheureusement, ce code est erroné.

+2

le NPE ne se produit pas dans votre main. il se produit dans 'java.util.Objects.requireNonNull (Objects.java:203)' – XtremeBaumer

Répondre

0
Finder finder = new Finder(pattern); 

Vous essayez d'instancier une classe statique. Vous devriez rendre le Finder non statique si vous voulez l'instancier avec un certain pattern.

+0

Check this out: https://docs.oracle.com/javase/tutorial/essential/io/examples/Find.java –

+0

Ce code fonctionne si nous supprimons la ligne: // private FileVisitResult CONTINUE; Aussi, changez "return CONTINUE;" dans les méthodes "SimpleFileVisitor" substituées à: return FileVisitResult.CONTINUE; –