【问题标题】:how to count the loc without block comments?如何在没有块注释的情况下计算 loc?
【发布时间】:2013-01-13 19:14:37
【问题描述】:

我正在尝试从 java 文件中获取代码行数。但我数不清。

首先我尝试用 ifs 跳过它们,但我的想法行不通。现在我用 cmets 计算相同的行,我的 Java 文件有这个头。任何想法,我都被困在如何计算它们。

我的 if 用于获取 cmets 块的行数。我试着做一个减法。

/*
example
example
*/

int totalLoc = 0;
int difference = 0;
while((line =buff.readLine()) !=null){

    if((line.trim().length() !=0 &&(!line.contains("/*") ||!line.contains("*/")) )){
       if(line.startsWith("/*")){
           difference++;
       }else if(linea.startsWith("*/")){
           difference++;
       }else{
           difference++;
       }
    }
}

【问题讨论】:

  • 您只对多线 cmets 感兴趣吗?没有一个 cmets 在他们之前会有任何东西(包括空格)吗?是否所有 3 个“分支”(包括默认分支)都应该做同样的事情?
  • 是的,我对多行 cmets 感兴趣,是的,只是空格,我已经有了像 /* something*/ 这样的 cmets,
  • 我对逻辑的建议是修剪在行上使用拆分来判断 /* 是在开头还是结尾甚至中间,并将布尔函数关联到 true 并且不计算行,直到你找到结束标签(再做一次拆分,看看标签后面是否有有效内容。

标签: java lines-of-code block-comments


【解决方案1】:

http://ostermiller.org/findcomment.html 查看此链接,它将为您提供更多帮助。 并通过使用此表达式 => (/*([^]|[\r\n]|(*+([^/]|[\r\n]))) *+/)|(//.) 你可以同时计算 cmets 单行和多行!!

【讨论】:

  • 欢迎来到 Stack Overflow。请在您的答案中总结链接;这样,如果链接过时,答案也不会完全没用。
【解决方案2】:

得到解决方案试试下面的代码,它将打印文件中找到的所有多行 cmets 以及多行 cmets 的总行数。

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.charset.Charset;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

public class LinesOfCode {
    public static void main(String[] args) {
        try {
            String s = readFile("D:\\src\\SampleClass.java");

            Pattern p = Pattern.compile("/\\*[\\s\\S]*?\\*/");

            Matcher m = p.matcher(s);

            int total = 0;
            while (m.find()) {

                String lines[] = m.group(0).split("\n");
                for (String string : lines) {
                    System.out.println(string);
                    total++;
                }
            }
            System.out.println("Total line for comments = " + total);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String readFile(String path) throws IOException {
        FileInputStream stream = new FileInputStream(new File(path));
        try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
                    fc.size());
            /* Instead of using default, pass in a decoder. */
            return Charset.defaultCharset().decode(bb).toString();
        } finally {
            stream.close();
        }
    }

}

【讨论】:

  • 如果您正在寻找其他解决方案,请告诉我。
【解决方案3】:

如果您想计算任何文件中的行数,请在下面的方法中写入并将文件名作为输入传递给下面的方法,它将返回计数。

public int count(String filename) throws IOException
     {
        InputStream is = new BufferedInputStream(new FileInputStream(filename));
        try
        {
            byte[] c = new byte[1024];
            int count = 0;
            int readChars = 0;
            boolean empty = true;
            while ((readChars = is.read(c)) != -1)
            {
                empty = false;
                for (int i = 0; i < readChars; ++i)
                {
                    if (c[i] == '\n')
                        ++count;
                }
            }
            return (count == 0 && !empty) ? 1 : count;
        }
        finally
        {
            is.close();
        }
    }

【讨论】:

  • 如果您正在寻找其他东西,请告诉我。
  • 但我想计算 cmets 块 cmets,这导致头痛
  • 别担心放松尝试应用逻辑我相信你会得到解决方案。一旦找到解决方案,我也会应用逻辑,我将再次发布我的答案我的声誉非常低,因此我无法修改您的问题,只需修改问题并添加即可。 “我只想找到注释行”。放示例注释行。
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 2022-10-13
相关资源
最近更新 更多