我想要提取单行和多行注释 [英] I want extract single line and multi line comments

查看:72
本文介绍了我想要提取单行和多行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它无法提取下面的评论

/ *

这是第一个程序

* /



和它一样

/ *





* /



我的尝试:



it can not extract the comments like bellow
/*
this is the first program
*/

and it gives like
/*


*/

What I have tried:

package javaapplication5;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * BufferedReader and Scanner can be used to read line by line from any File or
 * console in Java.
 * This Java program demonstrate line by line reading using BufferedReader in Java
 *
 * @author Javin Paul
 */
public class BufferedReaderExample {   

    public static void main(String args[]) throws IOException{
      
        //reading file line by line in Java using BufferedReader       
        FileInputStream fis = null;
        BufferedReader br = null;
        //String line;
        try {
            fis = new FileInputStream("E:/Sum.txt");
            br = new BufferedReader(new InputStreamReader(fis));
            String line;
            while((line = br.readLine()) != null){
                if(line.contains("/*") || line.contains("*/")|| line.contains("//")){
                    
                    System.out.println(line);
                    
                }
                
                else
                   
                    System.out.println("");
                
            }
            br.close();
          
            
  }
        catch(IOException e){
            System.out.println("file could not read");
            
        }
    }
}

推荐答案

陈旧的C风格评论可以跨越多行,因此如果您在注释中,则可以使用布尔标志,如果是,则需要输出注释中的行。
The old, C-style comments can span more than one line so you could have a boolean flag that is true if you are in a comment and if so you need to output the lines within the comment.


这篇关于我想要提取单行和多行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆