-1

I don't have much expirience in Java programming, and I tried to write simple program that iterates through some directory and its subfolders and counts txt files. I have used this code:

import java.io.*;
public class Fajlovi {

    public static void main(String[] args) throws Exception  {

        Fajlovi n=new Fajlovi();
        File f=new File("D:\\");

        System.out.println("Number of txt files is"+n.listaj(f));

    }

    public int listaj(File f){
        int count=0;
        File[] s= f.listFiles();

        for(int i=0;i<s.length;i++){

            if(s[i].isDirectory())
                count+=listaj(s[i]);
            else if(s[i].getName().endsWith(".txt")){
                count++;

            }
        }
        return count;
    }
}

Problem is that it works sometimes depending which directory I specify, but often it throws NullPointerException and stops execution. I have used some commands to follow execution steps, and I found out that it stops when count+=listaj(s[i]) is called for some directory that I even can't find on my computer and its not hidden.

4

4 回答 4

1

It throws NullPointerException when your program is trying to access those secure directories which comes under the System Files and folder. In your code when s[i].isDirectory() becomes true then it starts exploring s[i] directory and as the JVM doesn't have access to that secure location it returns null value. You can check whether the directory is null or not and then only try to find .txt file in that directory. Hope this explanation answer your question.

于 2013-08-14T17:10:44.220 回答
0

File.listFiles() will return null if it is invoked on a file that is not a directory. If that happens, your loop condition will cause the NullPointerException. This makes sense if it is being invoked, as you mention at the end, on something that does not exist.

Why this is happening requires more information. What is the "file that [you] can't even find on [your] computer?"

于 2013-08-14T16:35:30.433 回答
0

Your program crashes when there is an invalid directory/file used.

From the documentation about listFiles():

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

This code should prevent future NullPointerExceptions:

File[] s = f.listFiles();
if (s != null) {
    for (int i = 0; i < s.length; i++) {

        if (s[i].isDirectory())
            count += listaj(s[i]);
        else if (s[i].getName().endsWith(".txt")) {
            count++;

        }
    }
    return count;
} else {
    System.out.println("Invalid directory");
    return 0;
}
于 2013-08-14T16:35:32.047 回答
0

This approach worked for me (running on Win 7):

import java.io.*;

public class txtCount
{

  public static void main(String[] args)
  {
     txtCount n=new txtCount();
     File f=new File("D:\\");

     System.out.println("Number of txt files is " + n.listAndCount(f));
  }

   public int listAndCount(File f)
   {
       int count=0;
       for (final File fileEntry : f.listFiles()) 
       {
          if (fileEntry.isDirectory())
          {
             count += listAndCount(fileEntry);
          }
          else if(fileEntry.getName().endsWith(".txt"))
          {
              count++;
          }
       }
       return count;
    }
}
于 2013-08-14T17:15:12.430 回答