Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在没有区块注释的情况下计算loc?_Java_Lines Of Code_Block Comments - Fatal编程技术网

Java 如何在没有区块注释的情况下计算loc?

Java 如何在没有区块注释的情况下计算loc?,java,lines-of-code,block-comments,Java,Lines Of Code,Block Comments,我试图从java文件中获取代码行数。但我在数它们时遇到了麻烦 首先,我试图用ifs跳过它们,但我的想法行不通。现在我正在计算带有注释的相同行,我的Java文件有这个头。任何想法,我都被困在如何计算它们 我的if用于获取带有注释块的行数。我想做一个减法 /* example example */ int totalLoc = 0; int difference = 0; while((line =buff.readLine()) !=null){ if((line.trim().len

我试图从java文件中获取代码行数。但我在数它们时遇到了麻烦

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

我的if用于获取带有注释块的行数。我想做一个减法

/*
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++;
       }
    }
}

如果要计算任何文件中的行数,请将“写在下面”方法中的文件名作为输入传递给“下面”方法,该方法将返回计数

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();
        }
    }
public int count(字符串文件名)引发IOException
{
InputStream is=new BufferedInputStream(new FileInputStream(filename));
尝试
{
字节[]c=新字节[1024];
整数计数=0;
int readChars=0;
布尔空=真;
而((readChars=is.read(c))!=-1)
{
空=假;
for(int i=0;i
如果要计算任何文件中的行数,请使用“写在下面”方法,并将文件名作为输入传递给“下面”方法,该方法将返回计数

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();
        }
    }
public int count(字符串文件名)引发IOException
{
InputStream is=new BufferedInputStream(new FileInputStream(filename));
尝试
{
字节[]c=新字节[1024];
整数计数=0;
int readChars=0;
布尔空=真;
而((readChars=is.read(c))!=-1)
{
空=假;
for(int i=0;i
获得了解决方案,请尝试下面的代码。它将打印文件中的所有多行注释以及多行注释的总行数

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

}

获得了解决方案请尝试下面的代码它将打印所有多行注释以及在文件中找到的多行注释的总行数

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

}
查看此链接,它将为您提供更多帮助。 通过使用此表达式=>(/*([^]|[\r\n]|(*+([^/]|[\r\n]))*+/)|(/) 您可以计算单行和多行注释

查看此链接,它将为您提供更多帮助。 通过使用此表达式=>(/*([^]|[\r\n]|(*+([^/]|[\r\n]))*+/)|(/)
您可以计算单行和多行注释

你只对多行评论感兴趣吗?所有评论前面都不会有任何内容(包括空格)?所有3个“分支”(包括默认分支)都应该做同样的事情吗?是的,我对多行注释感兴趣,是的,只是空格,我已经有了类似/*something*/,,我在这里对逻辑的建议是,在行上使用一个拆分来判断/*是在开头还是结尾,甚至是中间,并将布尔函数与true相关联,在找到结束标记之前不要对行进行计数(进行另一次拆分以查看标记后面是否有有效内容。您是否只对多行注释感兴趣?是否所有注释前面都没有任何内容(包括空格)?都是3个“分支”(包括默认值)应该做同样的事情吗?是的,我对多行注释感兴趣,是的,只是空格,我已经有了类似于/*某物*/的注释,我在这里对逻辑的建议是修剪,在行上使用拆分来判断/*是在开始还是结束,甚至是中间,并将布尔函数与true关联,在找到结束语(进行另一次拆分,以查看标记后是否有有效内容。如果您正在查找其他内容,请告诉我。但我想计算评论阻止评论,这会导致头痛不要担心放松尝试应用逻辑我相信您会得到解决方案。我也在应用逻辑一旦找到解决方案,我将再次发布我的答案我的声誉评论非常少,因此我无法修改您的问题,只需修改问题并添加“我只想找到评论行”.举例说明注释行。如果您正在寻找其他内容,请告诉我。但我想计算注释阻止注释,这会导致头痛。不要担心,放松尝试应用逻辑我相信您会得到解决方案。我也在应用逻辑,一旦找到解决方案,我将再次发布我的答案。我的声誉很低,因此我无法o修改您的问题只需修改问题并添加“我只想找到已评论的行”.put example commented line.如果您正在寻找其他解决方案,请告诉我。如果您正在寻找其他解决方案,请告诉我。欢迎使用Stack Overflow。请在您的答案中总结链接;这样,如果链接过时,答案不会完全无用。欢迎使用Stack Overflow。请在您的答案中总结链接;这样,如果链接过时,答案也不会完全无用。