有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建用于删除文件中注释的工具?

我是Java编程新手。我想创建一个Java程序,它扫描整个目录树,搜索注释并删除它们。我发现这个代码:

删除评论:

import java.io.*;

public class RemoveComments {
    public void readAndPrintFile(String fileName) {
        int ch;
        boolean tokenCheck = false;
        boolean slashCommentFound = false;
        boolean starCommentFound = false;
        boolean firstSlashFound = false;
        boolean firstStarFound = false;
        boolean closingStarFound = false;
        boolean startDoubleQuoteFound = false;
        int lastChar;

        try {

            BufferedReader reader = new BufferedReader(new FileReader(fileName));

            while ((ch = reader.read()) != -1) {

                lastChar = ch;

                if (ch == '\"') {
                    if (startDoubleQuoteFound == false) {
                        startDoubleQuoteFound = true;
                    } else if (startDoubleQuoteFound == true) {
                        startDoubleQuoteFound = false;
                    }
                }

                if (startDoubleQuoteFound
                        && (starCommentFound == true || slashCommentFound == true)) {
                    continue;
                }
                if (ch == '/') {
                    if (starCommentFound == true && closingStarFound == false) {
                        continue;
                    }
                    if (closingStarFound && starCommentFound == true) {
                        starCommentFound = false;
                        closingStarFound = false;
                        firstStarFound = false;
                        continue;
                    } else if (firstSlashFound && slashCommentFound == false
                            && starCommentFound == false) {
                        slashCommentFound = true;
                        firstSlashFound = false;
                        continue;
                    } else if (slashCommentFound == false
                            && starCommentFound == false
                            && startDoubleQuoteFound == false) {
                        firstSlashFound = true;
                        continue;
                    }
                }
                if (ch == '*') {
                    if (starCommentFound) {
                        closingStarFound = true;
                        continue;
                    }
                    if (firstSlashFound && starCommentFound == false) {
                        starCommentFound = true;
                        firstSlashFound = false;
                        continue;
                    } else if (firstStarFound == false
                            && starCommentFound == true) {
                        firstStarFound = true;
                        continue;
                    }
                }
                if (ch == '\n') {
                    if (slashCommentFound) {
                        slashCommentFound = false;
                        firstStarFound = false;
                        firstSlashFound = false;
                        starCommentFound = false;
                        System.out.print((char) ch);
                        continue;
                    }
                }

                if (starCommentFound == true && closingStarFound == false) {
                    continue;
                }

                if (ch != '/' && ch != '*') {
                    if (closingStarFound) {
                        System.out.print((char) lastChar);
                    }

                    closingStarFound = false;
                    firstSlashFound = false;
                    firstStarFound = false;
                    closingStarFound = false;

                }

                if (slashCommentFound == false && starCommentFound == false) {
                    System.out.print((char) ch);
                }
            }

            reader.close();

        } catch (FileNotFoundException ex) {
            System.out.println(fileName + " not found");
        } catch (Exception ex) {
            System.out.println("Error reading file " + fileName);
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        RemoveComments reader = new RemoveComments();
        reader.readAndPrintFile("D://Callback.java");
    }
}

下面是列出目录树的代码:

public static void main(String[] args) {
    final Collection<File> all = new ArrayList<File>();
    addFilesRecursively(new File("."), all);
    System.out.println(all);
}

private static void addFilesRecursively(File file, Collection<File> all) {
    final File[] children = file.listFiles();
    if (children != null) {
        for (File child : children) {
            all.add(child);
            addFilesRecursively(child, all);
        }
    }
}

上面的代码适用于java源代码文件,但我不确定它是否适用于XML文件、CSS文件、Javascript和HTML文件。我还需要一个帮助,以正确的方式编写代码

祝福


共 (0) 个答案