0

我是 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 文件。我还需要帮助以正确的方式编写代码。

最好的祝愿

4

0 回答 0