开发者

How do i read in a file with buffer reader but skip comments with java

I wrote a program that reads a file with buffer reader and stores data in a String variable. How can i modify it to make it skip single and multi-line comments?

Here is my code:

import java.util.*;
import java.io.*;

public class IfCounter 开发者_如何转开发
{
    public static void main(String[] args) throws IOException
    {
        // parameter the TA will pass in
        String fileName = args[0];

        // variable to keep track of number of if's
        int ifCount = 0;

        // create a new BufferReader
        BufferedReader reader = new BufferedReader( new FileReader (fileName));
        String line  = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        // read from the text file
        while (( line = reader.readLine()) != null) 
        {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }

        // create a new string with stringBuilder data
        String tempString = stringBuilder.toString();

        // create one last string to look for our valid if(s) in
        // with ALL whitespace removed
        String compareString = tempString.replaceAll("\\s","");

        // check for valid if(s)
        for (int i = 0; i < compareString.length(); i++)
        {
            if (compareString.charAt(i) == ';' || compareString.charAt(i) == '}' || compareString.charAt(i) == '{') // added opening "{" for nested ifs :)
            {
                i++;

                if (compareString.charAt(i) == 'i')
                {
                    i++;

                    if (compareString.charAt(i) == 'f')
                    {
                        i++;

                        if (compareString.charAt(i) == '(')
                            ifCount++;
                    } // end if
                } // end if
            } // end if

        } // end for

        // print the number of valid "if(s) with a new line after"
        System.out.println(ifCount + "\n");

    } // end main
} // end class


Change this:

    while (( line = reader.readLine()) != null) {
      stringBuilder.append(line);
      stringBuilder.append(ls);
    }

to this:

    boolean multiLineComment = false;
    while (( line = reader.readLine()) != null) {
      if (!isLineAMultiLineCommentStart(line)) {
        multiLineComment = true;
      }

      if (multiLineComment) {
        if (!isLineAMultiLineCommentEnd(line)) {
          multiLineComment = false;
        }
      }

      if (!isLineAComment(line) && !multiLineComment) {
        stringBuilder.append(line);
        stringBuilder.append(ls);
      }
    }

You'll need to create a boolean methods, isLineAComment(String line), isLineAMultiLineCommentStart, and isLineAMultiLineCommentEnd but this should be easy for you to do.


Your question doesn't specify what the input language is, and without that it is not possible to give a complete answer. (For instance, if the input language was Fortran IV, you would just look for a 'C' in column 6. Does that answer satisfy you?)

The general answer is that accurate comment stripping usually requires you to implement (at least) a partial lexical analyzer for the input language. For instance, in Java accurate comment stripping needs to deal with:

  • // comments in the middle of a line
  • /* ... */ comments spanning multiple lines
  • comments where the / or * characters are expressed as Unicode escapes
  • // or /* or */ embedded in string literals

There quite a lot to get right there ...


If you are actually attempting to do this to analyze Java source code, a better idea would be to use an existing Java parser / AST analysis framework. For instance, PMD has a nice framework for doing this kind of thing ... and I'm sure that there are other alternatives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜