我可以在java中读取文件并打印其内容而无需注释语句吗?

时间:2015-12-22 22:17:34

标签: java

当我将java文件作为令牌阅读并打印出来时, 使用BufferedReader和StringTokenizer,我如何只打印其内容而没有以"开头的注释语句。 //" ," / * * /" 。 我想打印文件内容而没有用于澄清代码的这些语句。

3 个答案:

答案 0 :(得分:0)

1如果你想删除评论,你可以:

  • 删除// =>在这里看到相同的问题,不需要正则表达式:Find single line comments in byte array

  • 删除/ * * /它更难。正则表达式可以工作,但你可能会受到很多痛苦。我不推荐

2使用java解析器:Java : parse java source code, extract methods

javaparser例如:https://github.com/javaparser/javaparser

然后迭代代码,删除注释等。

答案 1 :(得分:0)

您可以使用JavaParser轻松完成此操作:只需解析代码,指定您要忽略注释然后转储AST

CompilationUnit cu = JavaParser.parse(reader, false /*considerComments*/);
String codeWithoutComments = cu.toString();

转储时会重新格式化代码。

答案 2 :(得分:0)

此代码将删除文本文件中的注释,但是不会删除注释的符号,如果需要删除注释,可以通过编辑下面编写的三个函数来完成。我已经测试过。

// helloworld
/* comment */
a  /* comment */
b
/*
comment
*/
c
d
e
// xxxx
f // xxxx

输出将是:

//
/* */
a  /* */
b
/*

*/
c
d
e
//
f //

在此程序中,我并未像创建词法分析器时那样删除注释符号。您可以通过编辑放置注释的程序语句来删除注释符号。

public class testSpace {
public static void main(String[] args) {
    try {
        String filePath = "C:\\Users\\Sibil\\eclipse-workspace\\Assignment1\\src\\Input.txt";
        FileReader fr = new FileReader(filePath);
        String line;
        BufferedReader br = new BufferedReader(fr);
        int lineNumber = 0;
        while ((line = br.readLine()) != null) {
            lineNumber++;
            if ((line.contains("/*") && line.contains("*/")) || (line.contains("//"))) {
                line = findreplacement(line);
                System.out.println(line);//Begining of the multiline comment
            } else if (line.contains("/*")) {
                line = getStartString(line);
                System.out.println(line);
                while ((line = br.readLine()) != null) {
                    lineNumber++;
                    if (line.contains("*/")) {
                        line = getEndString(line);
                        System.out.println(line);//Print the end of a Multline comment
                        break;
                    } else {
                        line = " ";
                        System.out.println(line);//Blank Space for commented line inside a multiline comment 
                    }
                }
            } else
                System.out.println(line);//Line without comment
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

private static String getEndString(String s) {
    int end = s.indexOf("*/");
    String lineEnd = s.substring(end, s.length());//Edit here if you don't need the comment symbol by substracting 2 or adding 2
    return lineEnd;
}

private static String getStartString(String s) {
    int start = s.indexOf("/*");
    String lineStart = s.substring(0, start + 2);//Edit here if you don't need the comment symbol by substracting 2 or adding 2
    return lineStart;
}

private static String findreplacement(String s) {
    String line = "";
    if (s.contains("//")) {
        int start = s.indexOf("//");
        line = s.substring(0, start + 2);//Edit here if you don't need the comment symbol by substracting 2 or adding 2
    } else if ((s.contains("/*") && s.contains("*/"))) {
        int start = s.indexOf("/*");
        int end = s.indexOf("*/");
        String lineStart = s.substring(0, start + 2);//Edit here if you don't need the comment symbol by substracting 2 or adding 2
        String lineEnd = s.substring(end, s.length());//Edit here if you don't need the comment symbol by substracting 2 or adding 2
        line = lineStart + " " + lineEnd;
    }
    return line;
}
}

如果文件中有这样一行,

System.out.println("Hello World/*Do Something */");

它将失败,输出将是:

System.out.println("Hello world");