Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:31:04+00:00 2026-06-06T17:31:04+00:00

I have developed a application in which user select the particular folder and it

  • 0

I have developed a application in which user select the particular folder and it counts all the java files in that folder plus the line of code individually in those files and show at console but in a java project there are so many packages and right now I have to navigate until a particular package , I want to modify the application in such a way that when user select the particular project, he will then further navigate to only src folder and from src folder all the packages containing java files line of code will be counted.

public class abc {

    /**
     * @param args
     * @throws FileNotFoundException
     */
    private static int totalLineCount = 0;
    private static int totalFileScannedCount = 0;

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

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
        chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            Map<String, Integer> result = new HashMap<String, Integer>();
            File directory = new File(chooser.getSelectedFile().getAbsolutePath());

            List<File> files = getFileListing(directory);

            //print out all file names, in the the order of File.compareTo()
            for (File file : files) {
                System.out.println("Directory: "+file);
                result = getFileLineCount(file);
                totalFileScannedCount +=  result.size();
            }


            System.out.println("*****************************************");
            System.out.println("FILE NAME FOLLOWED BY LOC");
            System.out.println("*****************************************");

            for (Map.Entry<String, Integer> entry : result.entrySet()) {
                System.out.println(entry.getKey() + " ==> " + entry.getValue());
            }
            System.out.println("*****************************************");
            System.out.println("SUM OF FILES SCANNED ==>" + "\t" + totalFileScannedCount);
            System.out.println("SUM OF ALL THE LINES ==>" + "\t" + totalLineCount);

        }

    }

    public static Map<String, Integer> getFileLineCount(File directory) throws FileNotFoundException {
        Map<String, Integer> result = new HashMap<String, Integer>();

        File[] files = directory.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File directory, String name) {
                if (name.endsWith(".java")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        for (File file : files) {
            if (file.isFile()) {
                Scanner scanner = new Scanner(new FileReader(file));
                int lineCount = 0;
                try {
                    for (lineCount = 0; scanner.nextLine() != null; lineCount++);
                } catch (NoSuchElementException e) {
                    result.put(file.getName(), lineCount);
                    totalLineCount += lineCount;
                }
            }
        }

        return result;
    }

    /**
     * Recursively walk a directory tree and return a List of all
     * Files found; the List is sorted using File.compareTo().
     *
     * @param aStartingDir is a valid directory, which can be read.
     */
    static public List<File> getFileListing(
            File aStartingDir) throws FileNotFoundException {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir);
        Collections.sort(result);
        return result;
    }

    // PRIVATE //
    static private List<File> getFileListingNoSort(
            File aStartingDir) throws FileNotFoundException {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        for (File file : filesDirs) {
            if(file.isDirectory()) {
                result.add(file); 
            }
            if (!file.isFile()) {
                //must be a directory
                //recursive call!
                List<File> deeperList = getFileListingNoSort(file);
                result.addAll(deeperList);
            }
        }
        return result;
    }

    /**
     * Directory is valid if it exists, does not represent a file, and can be read.
     */
    static private void validateDirectory(
            File aDirectory) throws FileNotFoundException {
        if (aDirectory == null) {
            throw new IllegalArgumentException("Directory should not be null.");
        }
        if (!aDirectory.exists()) {
            throw new FileNotFoundException("Directory does not exist: " + aDirectory);
        }
        if (!aDirectory.isDirectory()) {
            throw new IllegalArgumentException("Is not a directory: " + aDirectory);
        }
        if (!aDirectory.canRead()) {
            throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
        }
    }
}

Now the problem is the issue with this approach is that if in java project named TESTRESULT there are 3 packages and each package having 2 files each then in the result on console it shows file name followed by loc of only the latest package only lets say in a java projest 3 packages named abc, def and tyu

com.abc package having files --->abc.java
com.def package having files --->abc.java
com.tyu package having files --->FileBrowse.java , FileCountLine.java

the outcome shown in console is... 


    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\.settings
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\bin
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\abc
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\def
    Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\tyu
    *****************************************
    FILE NAME FOLLOWED BY LOC
    *****************************************
    FileBrowse.java ==> 95
    FileCountLine.java ==> 53
    *****************************************
    SUM OF FILES SCANNED ==> 4
    SUM OF ALL THE LINES ==> 296

which is not perfect. please advise HOW IT WILL SHOW FILENAME FOLLOWED BY LOC for all the files

What I think is In the method that reads the files and the line count, you create a NEW Map for each directory and re-assign it to the same result. Since you do not display the data before you re-assign the Map, the previous Map is no longer accessible and by the time you get to the display you only have a Map for the last directory. Instead, you should maintain a single Map and insert new values from each directory into the same Map.

public static void main(String[] args) throws FileNotFoundException {  
            //...  
            for (File file : files) {  
                System.out.println("Directory: "+file);  
                result = getFileLineCount(file);

Right there you get a Map back from getFileLineCount(), and assign it to result. This discards the previously created result Map, and you lose everything that was already there. You have a few options:

  1. Pass the result Map into the getFileLineCount() method so you can add results to the one Map, rather than creating a new Map for each folder inside the getFileLineCount() method
  2. Take the results from getLineCount() and copy them into the result Map rather than replacing the result Map
  3. Create another collection, like a Map like: Map<String, Map<String, Integer>> (which would map the results returned by the getFileLineCount() method to the name of the directory those results pertain to), or List<Map<String, Integer>> (which would be a simple list of the results returned by getFileLineCount() without any mapping to the parent directory).

Please advise how to refactor my code ,Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:31:07+00:00Added an answer on June 6, 2026 at 5:31 pm

    You could do something like this:

    result.put(file, lineCount);
    

    instead of

    result.put(file.getName(), lineCount);
    

    And when you print the result you could print the pretty file name. Like this:

    for (Map.Entry<File, Integer> entry : result.entrySet()) {
        System.out.println(entry.getKey().getName() + " ==> " + entry.getValue());
    }
    

    Of course map should be then declared as: Map.Entry<File, Integer>


    Of course this can be improved a lot but with minimal changes you could have this (that is actually doing what you expect, please just change to print the name if you don’t want the entire path):

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FilenameFilter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    
    import javax.swing.JFileChooser;
    
    public class abc {
    
        /**
         * @param args
         * @throws FileNotFoundException
         */
        private static int totalLineCount = 0;
        private static int totalFileScannedCount = 0;
    
        public static void main(final String[] args) throws FileNotFoundException {
    
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
            chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                Map<File, Integer> result = new HashMap<File, Integer>();
                File directory = new File(chooser.getSelectedFile().getAbsolutePath());
    
                List<File> files = getFileListing(directory);
    
                // print out all file names, in the the order of File.compareTo()
                for (File file : files) {
                    System.out.println("Directory: " + file);
                    getFileLineCount(result, file);
                }
    
                System.out.println("*****************************************");
                System.out.println("FILE NAME FOLLOWED BY LOC");
                System.out.println("*****************************************");
    
                for (Map.Entry<File, Integer> entry : result.entrySet()) {
                    System.out.println(entry.getKey().getAbsolutePath() + " ==> " + entry.getValue());
                }
                System.out.println("*****************************************");
                System.out.println("SUM OF FILES SCANNED ==>" + "\t" + totalFileScannedCount);
                System.out.println("SUM OF ALL THE LINES ==>" + "\t" + totalLineCount);
            }
    
        }
    
        public static void getFileLineCount(final Map<File, Integer> result, final File directory)
                throws FileNotFoundException {
            File[] files = directory.listFiles(new FilenameFilter() {
    
                public boolean accept(final File directory, final String name) {
                    if (name.endsWith(".java")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            for (File file : files) {
                if (file.isFile()) {
                    totalFileScannedCount++;
                    Scanner scanner = new Scanner(new FileReader(file));
                    int lineCount = 0;
                    try {
                        for (lineCount = 0; scanner.nextLine() != null; lineCount++) {
                            ;
                        }
                    } catch (NoSuchElementException e) {
                        result.put(file, lineCount);
                        totalLineCount += lineCount;
                    }
                }
            }
    
        }
    
        /**
         * Recursively walk a directory tree and return a List of all Files found;
         * the List is sorted using File.compareTo().
         * 
         * @param aStartingDir
         *            is a valid directory, which can be read.
         */
        static public List<File> getFileListing(final File aStartingDir) throws FileNotFoundException {
            validateDirectory(aStartingDir);
            List<File> result = getFileListingNoSort(aStartingDir);
            Collections.sort(result);
            return result;
        }
    
        // PRIVATE //
        static private List<File> getFileListingNoSort(final File aStartingDir) throws FileNotFoundException {
            List<File> result = new ArrayList<File>();
            File[] filesAndDirs = aStartingDir.listFiles();
            List<File> filesDirs = Arrays.asList(filesAndDirs);
            for (File file : filesDirs) {
                if (file.isDirectory()) {
                    result.add(file);
                }
                if (!file.isFile()) {
                    // must be a directory
                    // recursive call!
                    List<File> deeperList = getFileListingNoSort(file);
                    result.addAll(deeperList);
                }
            }
            return result;
        }
    
        /**
         * Directory is valid if it exists, does not represent a file, and can be
         * read.
         */
        static private void validateDirectory(final File aDirectory) throws FileNotFoundException {
            if (aDirectory == null) {
                throw new IllegalArgumentException("Directory should not be null.");
            }
            if (!aDirectory.exists()) {
                throw new FileNotFoundException("Directory does not exist: " + aDirectory);
            }
            if (!aDirectory.isDirectory()) {
                throw new IllegalArgumentException("Is not a directory: " + aDirectory);
            }
            if (!aDirectory.canRead()) {
                throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a application in which user select the particular folder and it
Hai I have developed a standalone application in which when an user logs in,
We have developed a web application which consumes a web service. The user will
I have developed an application which can create Xml files from Xml schema with
I have developed web application which uses JasperReports for reporting purpose. In that I
I have developed a web based application which allows a client/user to upload a
I have developed a grails/groovy application for a legacy database which has user maintenance
I have developed one application in which i have to detect whether user has
I have developed an application which allows the user to switch between themes. I'm
I have developed one application in which i need to register a user in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.