How to count lines in a netbeans project

2020-06-30 04:37发布

Hello I have netbeans 7 and I was wondering how to calculate the total lines for a project. I have looked through Google but every time I do it I only find dead ends or non working plugins. Does anyone know how to count the lines?

4条回答
不美不萌又怎样
2楼-- · 2020-06-30 04:47

I was hoping for a cut-and-paste answer. So I wrote one.

EDIT: Supports millions of lines of code. No external libraries required.

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

    final String folderPath = "D:\\Dev\\MYPROJECT\\src";

    long totalLineCount = 0;
    final List<File> folderList = new LinkedList<>();
    folderList.add(new File(folderPath));
    while (!folderList.isEmpty()) {
        final File folder = folderList.remove(0);
        if (folder.isDirectory() && folder.exists()) {
            System.out.println("Scanning " + folder.getName());
            final File[] fileList = folder.listFiles();
            for (final File file : fileList) {
                if (file.isDirectory()) {
                    folderList.add(file);
                } else if (file.getName().endsWith(".java")
                        || file.getName().endsWith(".sql")) {
                    long lineCount = 0;
                    final Scanner scanner = new Scanner(file);
                    while (scanner.hasNextLine()) {
                        scanner.nextLine();
                        lineCount++;
                    }
                    totalLineCount += lineCount;
                    final String lineCountString;
                    if (lineCount > 99999) {
                        lineCountString = "" + lineCount;
                    } else {
                        final String temp = ("     " + lineCount);
                        lineCountString = temp.substring(temp.length() - 5);
                    }
                    System.out.println(lineCountString + " lines in " + file.getName());
                }
            }
        }
    }
    System.out.println("Scan Complete: " + totalLineCount + " lines total");
}

The results appear similar to the following:

   (truncated)
   47 lines in WarningLevel.java
Scanning design
 1367 lines in ProcessResultsFrame.java
   83 lines in TableSettingPanel.java
Scanning images
Scanning settingspanel
   67 lines in AbstractSettingPanel.java
  215 lines in AdvancedSettingsPanel.java
   84 lines in BaseSettingsPanel.java
  451 lines in DatabasePanel.java
  488 lines in EmailPanel.java
  458 lines in FTPGUIPanel.java
  482 lines in FTPScheduledTaskPanel.java
  229 lines in GUISettingPanel.java
   87 lines in RootSettingJPanel.java
  722 lines in ServerVisualIdentificationSettingPanel.java
Scan Complete: 123685 lines total

If it's missing something please let me know and I'll do my best to correct it. Thanks!

查看更多
smile是对你的礼貌
3楼-- · 2020-06-30 04:53

You can use wordcount that works with 7.1 nb-wordcount that works with 8.2.

To configure wordcount go in Tools->Options->Miscellaneous. You have to change Accept filename if you want other files than Java and Groovy to match.

To display the count window go in Window->Open WordCount Window.

To display stats click on WordCounting (second button). I will display the stats of the directory selected in Projects (window)(it has to be a package or something like Source Packages or Web pages, it won't work if you select the project).

Also if you are on linux you can simply execute :

 find . -name '*.java' | xargs wc -l
查看更多
▲ chillily
4楼-- · 2020-06-30 05:08

I know this is a very old question however there is a simpler way of finding the line count in a netbeans project that doesn't involve installing plugins:

  1. Right click on the folder or package you want to find the amount of lines in
    Note: Don't right click on the project itself as that will cause it to count the lines in all the generated files too.
  2. Click on Find or Find in Files or press CtrlF.
  3. Make sure the Match dropdown is set to Regular Expression.
  4. Type in \n into the search box.
  5. Press find and the amount of lines your project has will be displayed at the top of the
    Search Results tab.

Note: In NetBeans, the search is stopped after 5000 results, so if your project is longer than that then this method won't work

查看更多
相关推荐>>
5楼-- · 2020-06-30 05:08

You could use Source Code Metrics for Java Projects.

查看更多
登录 后发表回答