3

我开发了一个应用程序,用户在其中选择特定文件夹,它计算该文件夹中的所有 java 文件以及这些文件中单独的代码行并显示在控制台中,但是在 java 项目中,有很多包,现在我有导航到一个特定的包,我想修改应用程序,当用户选择特定项目时,他将进一步导航到仅 src 文件夹,并且从 src 文件夹中所有包含 java 文件行的包都将被计算在内.

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);
        }
    }
}

现在的问题是这种方法的问题是,如果在名为 TESTRESULT 的 java 项目中有 3 个包并且每个包有 2 个文件,那么在控制台的结果中它显示文件名后跟 loc 仅最新包只让说一个名为 abc、def 和 tyu 的 java projest 3 包

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

这并不完美。请告知它将如何显示所有文件的 LOC 后跟的文件名

我的想法是在读取文件和行数的方法中,您为每个目录创建一个 NEW Map 并将其重新分配给相同的结果。由于您在重新分配地图之前不显示数据,因此以前的地图不再可访问,并且当您到达显示时,您只有最后一个目录的地图。相反,您应该维护一个 Map 并将每个目录中的新值插入到同一个 Map 中。

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

在那里你得到一个 Map 回来getFileLineCount(),并将其分配给结果。这会丢弃先前创建的结果 Map,并且您会丢失已经存在的所有内容。你有几个选择:

  1. 将结果 Map 传递给方法,以便您可以将结果添加到一个 Map,而不是为方法getFileLineCount()内的每个文件夹创建一个新 MapgetFileLineCount()
  2. 从中获取结果getLineCount()并将它们复制到结果映射中,而不是替换结果映射
  3. 创建另一个集合,如 Map 之类的:(Map<String, Map<String, Integer>>它将getFileLineCount()方法返回的结果映射到这些结果所属的目录的名称),或者List<Map<String, Integer>>(这将是返回的结果的简单列表,getFileLineCount()没有到父级的任何映射目录)。

请告知如何重构我的代码,在此先感谢

4

1 回答 1

3

你可以这样做:

result.put(file, lineCount);

代替

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

当您打印结果时,您可以打印漂亮的文件名。像这样:

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

当然 map 应该被声明为:Map.Entry<File, Integer>


当然,这可以得到很大的改进,但是只需进行最小的更改,您就可以拥有它(这实际上是您所期望的,如果您不想要整个路径,请更改以打印名称):

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);
        }
    }
}
于 2012-06-30T16:42:59.997 回答