-1

This is my code for counting all the files in my comp, the code has not stopped running and there are over 2000000 files, is this normal, or is the code in an infinite loop. THanks for all the help :)

import java.io.*;
import java.util.*;
//got the framework from this link: stackoverflow.com/questions/3154488
public class RFF {
    public static void main(String [] args) {
        File[] files = new File("/Users").listFiles();
        showFiles(files);
        System.out.println(size);
    }
    static File file1 = new File ("/Users/varun/Desktop/a.pdf");
    static double size = file1.length();

    static int i = 0;
    public static void showFiles(File[] files) {
        try { 
            for (File file: files) {
                if (file.isDirectory()) {
                    if (file.isFile() == true) 
                        i++;
                    else 
                        i = i;

                    if (file.length() > size)
                        size = file.length();
                    System.out.println("FileCount: " + i + ">>> FileSize: " +file.length() + " >>> FileName: " + file.getName() );
                    showFiles(file.listFiles()); // Calls same method again.
                } else {
                    i++;
                    if (file.length() > size)
                        size = file.length();
                    System.out.println("FileCount: " + i + ">>> FileSize: " + file.length() + " >>> FileName: " + file.getName() );
                }

            }
        } catch (NullPointerException e) {
            System.out.println ("Exception thrown :" + e);
        }


    }
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • If you have a spinning disk supporting 100 IOPS, 2 million files will take about 6 hours, possibly double depending on your file system. – Peter Lawrey Mar 05 '17 at 13:06
  • 1
    Do you have half files on your file system? If not, why do you use a double for the size? What is `i = i` supposed to do? Please, try cleaning your code, don't use static variables when you don't need to, indent your code properly, and use curly braces agains blocks of code systematically, even when the block only has one instruction. And don't catch NullPointerException. – JB Nizet Mar 05 '17 at 13:07
  • 2
    Your code may be following shortcuts to get "back above itself" at which point it may never stop – Richard Tingle Mar 05 '17 at 13:07
  • In your computer, look at the properties of the folder "users" it'll tell you how many files there is into, (the computer I use is only 2 months old) i Have 74360 files in the properties, and 78000 with your code, so it appears to work quick good – azro Mar 05 '17 at 13:11
  • @JBNizet Why should I not catch nullpointer exception. It ends at 5000 files without it – Varun Narayanan Chakravarthy Mar 05 '17 at 13:13
  • @VarunNarayananChakravarthy Then you need to find out why it stops at 5000 files without it. Ignoring bugs doesn't make them go away – Richard Tingle Mar 05 '17 at 13:15
  • 1
    Because a NullPointerException indicates that there is a bug in your code. You should understand why the exception is thrown, and fix the bug, instead of catching and ignoring the exception. – JB Nizet Mar 05 '17 at 13:15

1 Answers1

1

It is highly likely that your users directory contains a shortcut (aka symbolic link) to a folder higher in the path, your code will follow these to get to files it has already counted this will lead to an infinite link.

E.g.

-Users 
  - Test
     -ShortCutToUsers

See this stackoverflow question for more details of determining symbolic links:

Java 1.6 - determine symbolic links

If you're on java 7+ you can determine symbolic links as follows:

Files.isSymbolicLink(path)
Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77