-1

When i read java file as tokens and print it's content, using BufferedReader and StringTokenizer,how can i print only its content without comment statements that begin with " // " , " /* */" . I want to print content of file without these statement that used for clarify the code.

M. SW.
  • 19
  • 4
  • Can you help me . I wrote code to process it using String.equals ,but isnt work!. – M. SW. Dec 22 '15 at 22:23
  • See [Java - Regex - Remove comments](http://stackoverflow.com/questions/28411032/java-regex-remove-comments). –  Dec 22 '15 at 22:28
  • public static void main(String[]ss) { try { fileName = "C:\\filename.java"; FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); line = br.readLine(); while (line != null) { StringTokenizer stTokenizer = new StringTokenizer(line," ( . , ; { "); while (stTokenizer.hasMoreTokens()) { words = stTokenizer.nextToken(); System.out.println(words); } line = br.readLine();} } catch (IOException e) { e.printStackTrace(); } – M. SW. Dec 22 '15 at 22:56

3 Answers3

1

You can do that very easily using JavaParser: just parse the code specifying that you want to ignore comments and then dump the AST

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

While dumping it will reformat the code.

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
  • @Frederico Tomassetti I am trying this solution, but apparently there is no `parse()` method that accepts a reader and a boolean. Am I missing something or was the JavaParser code changed since you posted this solution? – undisp May 09 '18 at 15:52
  • Federico not Frederico :) The solution was posted over 2 years ago and there is a new release of JavaParser EVERY WEEK, so yes, the API changed in the meantime. At the time of this writing the last version is 3.6.4 and to disable attributing comments you can use the ParserConfiguration http://static.javadoc.io/com.github.javaparser/javaparser-core/3.6.3/com/github/javaparser/ParserConfiguration.html – Federico Tomassetti May 09 '18 at 16:02
0

1 If you want to remove comments, you can well:

  • remove // => see the same question here, no need of regex : Find single line comments in byte array

  • remove /* */ it is more difficult. regex could work, but you could get a lot of pain . I dont recommend that

2 use a java parser : Java : parse java source code, extract methods

javaparser for example: https://github.com/javaparser/javaparser

then iterate the code, and remove comments, etc.

Community
  • 1
  • 1
  • Trying to remove comments with a regex could work until you mix strings and comments or put comments inside comments. It could be definitely tricky. – Federico Tomassetti Jan 01 '16 at 13:40
0

This code will remove the comment inside a text file.But, It will not remove the symbols of comment, if you need to remove it, you can do it by editing the three functions which I had written below.Test case which i had tested.

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

The Output will be:

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

*/
c
d
e
//
f //

In this program I didn't remove the comment symbol as I was making lexical analyzer.You can remove the comment symbols by editing the program statements where i had put the comments.

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

If your file has a line like this,

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

It will fail and the output will be:

System.out.println("Hello world");
SJK
  • 16
  • 3