计算文本文件中的行数,不包括java中的注释和新行

时间:2014-12-15 00:22:10

标签: java text-files bufferedreader

尝试阅读文本文件并计算除多行之外的行数" / * * /"评论和一行评论" //"从计数和在文本文件中找到所有方法" myMethod(){}" 并将它们打印出来并在文本文件中找到方法的总数。文本文件将在执行java程序ex:" java MyCountLine C:\ myFolder \ myText.txt"。   我无法找到从文本文件中获取方法的任何方法。 所以这是我的代码:

import java.io.*;

public class LineCountss {

public static void main(String[] args) {

    File inFile = null;

    if (0 < args.length) {
        // text file will be passed during run time
        inFile = new File(args[0]);
    } else {
        System.out.println("Cant Find The File Specified : " + inFile);
    }

    BufferedReader br = null;

    String sCurrentLine = null;

    String func = null;

    int a = 0, b = 0, c = 0, k = 0;

    try {

        // passing the text file location for FileReader.

        br = new BufferedReader(new FileReader(inFile));

        // Looping through the text file

        while ((sCurrentLine = br.readLine()) != null) {

            // avoid multi-line comments and one line comments and new
            // lines.

            if ((sCurrentLine.startsWith("/*") && sCurrentLine
                    .endsWith("*/"))
                    || sCurrentLine.startsWith("//")
                    || sCurrentLine.isEmpty()
                    || (sCurrentLine.trim().matches("[{};]+"))) {

                // count the number of comment lines and new lines to
                // exclude it from count.

                b++;

                // Getting any function in the text file that start and end
                // with ().

            } else if (sCurrentLine.contains("\\(\\)\\{")) {

                func = sCurrentLine.trim();

                // printing the functions/methods

                System.out.println(func);

                // counting the number of functions/ methods found

                k++;

            } else {

                // printing the text file just for checking

                System.out.println(sCurrentLine);

                // count the total number of lines

                a++;

            }

        }

        // excluding the number of lines that has comments and new lines

        c = a - b;

        // printing the number of lines excluding comments and new lines

        System.out.println("Number of Lines are : " + c);

        // printing the number of lines of the functions/method found inside
        // the text file.

        System.out.println("Number of Functions are : " + k);

    }

    catch (IOException e) {

        System.out.println(e.getMessage());

    }

    finally {

        try {

            // close bufferReader

            if (br != null) {

                br.close();

            }

        } catch (IOException ex) {

            System.out.println(ex.getMessage());

        }

      }

    }
}

1 个答案:

答案 0 :(得分:0)

无法将String.contains()与正则表达式一起使用。你可以试试这个:

sCurrentLine.matches("[^()]+\\([^()]*\\)\\s*\\{?")

您也应该排除关键字(因此不会将其视为函数):

!sCurrentLine.matches("\\s*(if|switch|for|while|catch)\\s*\\(.+\\)")