Java 如何使用分隔符和创建文本文件中所有注释的输出

Java 如何使用分隔符和创建文本文件中所有注释的输出,java,Java,到目前为止,我创建的代码允许我获取java源文件并将文本输入到文本文件中。现在我需要做的是识别注释..比如//和/*。并输出到屏幕。我还需要确定两者之间的界线: 1: /***************************************** 2: ** This is my program // Weird comment! ** 3: *****************************************/ 7: /* Amount available */ 14: /

到目前为止,我创建的代码允许我获取java源文件并将文本输入到文本文件中。现在我需要做的是识别注释..比如//和/*。并输出到屏幕。我还需要确定两者之间的界线:

1: /*****************************************
2: ** This is my program // Weird comment! **
3: *****************************************/
7: /* Amount available */
14: /* This is my code */

我想你需要这样的东西:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Please enter the file name: ");
        String sourceFile = kb.nextLine();

        try {
            Scanner in = new Scanner(new File(sourceFile));
            String txtFile = sourceFile + ".txt";

            PrintWriter fileOut = new PrintWriter(txtFile);

            while(in.hasNextLine()) {
                String intoTxt = in.nextLine();
                fileOut.print(intoTxt);
                fileOut.println("");
            }

            fileOut.close();

            File file = new File(txtFile);

            Scanner text = new Scanner(file);
            CommentParser cp = new CommentParser();
            cp.extractComments(text);

        } catch( FileNotFoundException e ) {
            System.out.println("Sorry that file does not exist or is not accessible");
        }
    }

    public static class CommentParser {

        private boolean isCommentBlock = false;

        public static final String START_COMMENT_BLOCK = "/*";
        public static final String END_COMMENT_BLOCK = "*/";
        public static final String START_COMMENT_LINE = "//";

        public void extractComments(Scanner text) {

            int lineNum = 0;
            while(text.hasNextLine()) {

                String line = text.nextLine();
                if( line.contains(START_COMMENT_BLOCK) && !this.isCommentBlock) {

                    // if it is a block comment

                    int startIdx = line.indexOf(START_COMMENT_BLOCK);
                    int endIdx = line.indexOf(END_COMMENT_BLOCK);

                    if( endIdx == -1 ) {
                        isCommentBlock = true;
                        this.printLine(lineNum, line.substring(startIdx, line.length()));
                    } else {
                        this.printLine(lineNum, line.substring(startIdx, endIdx + END_COMMENT_BLOCK.length()));
                    }
                } else if( this.isCommentBlock ) {

                    // handle end of block comment, if it is in a following line

                    int endIdx = line.indexOf(END_COMMENT_BLOCK);
                    if( endIdx == -1 ){
                        this.printLine(lineNum, line);
                    } else {
                        this.printLine(lineNum, line.substring(0, endIdx + END_COMMENT_BLOCK.length()));
                        this.isCommentBlock = false;
                    }

                } else if( line.contains(START_COMMENT_LINE) && !this.isCommentBlock ){

                    // if it is just a line comment

                    int startIdx = line.indexOf(START_COMMENT_LINE);
                    this.printLine(lineNum, line.substring(startIdx, line.length()));
                }

            lineNum++;
            }
        }

        private void printLine(int lineNum, String comment) {
            System.out.println(String.format("%s: %s", lineNum, comment));
        }
    }
}

您必须添加一些代码来识别一行中的几个块注释。这只是这段代码片段中的一个小补充

请添加代码而不是屏幕截图