如何使用缓冲读取器跳过注释?

时间:2011-09-13 02:54:29

标签: java

我编写了以下程序来读取文件并跳过注释,它适用于单行注释,但不适用于多行注释。有谁知道为什么?我不需要担心字符串中的“//”。只有java评论,即“//”和“/ * * /”

代码:

import java.io.*;

public class IfCounter2 
{   
    public static boolean lineAComment(String line)
    {
        if (line.contains("//"))    
            return true;

        return false;
    }

    public static boolean multiLineCommentStart(String line)
    {
        if (line.contains("/*"))    
            return true;

        return false;
    }

    public static boolean multiLineCommentEnd(String line)
    {
        if (line.contains("*/"))    
            return true;

        return false;
    }

    public static void main(String[] args) throws IOException 
    {
        String fileName = args[0];

        int numArgs = args.length;

        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");

        line = reader.readLine();
        // read from the text file
        boolean multiLineComment = false;

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

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

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


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

    } 
} 

3 个答案:

答案 0 :(得分:2)

multiLineComment为真时,您只需将!multiLineCommentStart(line)设置为true - 也就是说,只要该行不包含/*

答案 1 :(得分:0)

基本上,你的代码看起来应该是这样的(未经测试的)

    boolean multiLineComment = false;

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

      if (multiLineComment) {
        if (multiLineCommentEnd(line)) 
        {
          multiLineComment = false;
        }
      }

      if (!lineAComment(line) && (multiLineComment == false)) 
      {
        stringBuilder.append(line);
        stringBuilder.append(ls);
      }
    }

在最后一个if语句中,你需要一个包含变量和固定

的表达式

答案 2 :(得分:0)

Andy的回答是正确的,但最后需要验证才能确保你没有计算* /作为一个有效的行:

    boolean multiLineComment = false;

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

      if (multiLineComment) {
        if (multiLineCommentEnd(line)) 
        {
          multiLineComment = false;
        }
      }

      if (!lineAComment(line) && (multiLineComment == false) &&
           !multiLineCommentEnd(line) ) 
      {
        stringBuilder.append(line);
        stringBuilder.append(ls);
      }
    }