How to create a tool for removing comments in files?
<p>I'm new to Java programming. I want to create Java program which scans entire directory tree, searches for comments and removes them. I found this code:</p>

<p>remove comments:</p>

<pre><code>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
                        &amp;&amp; (starCommentFound == true || slashCommentFound == true)) {
                    continue;
                }
                if (ch == '/') {
                    if (starCommentFound == true &amp;&amp; closingStarFound == false) {
                        continue;
                    }
                    if (closingStarFound &amp;&amp; starCommentFound == true) {
                        starCommentFound = false;
                        closingStarFound = false;
                        firstStarFound = false;
                        continue;
                    } else if (firstSlashFound &amp;&amp; slashCommentFound == false
                            &amp;&amp; starCommentFound == false) {
                        slashCommentFound = true;
                        firstSlashFound = false;
                        continue;
                    } else if (slashCommentFound == false
                            &amp;&amp; starCommentFound == false
                            &amp;&amp; startDoubleQuoteFound == false) {
                        firstSlashFound = true;
                        continue;
                    }
                }
                if (ch == '*') {
                    if (starCommentFound) {
                        closingStarFound = true;
                        continue;
                    }
                    if (firstSlashFound &amp;&amp; starCommentFound == false) {
                        starCommentFound = true;
                        firstSlashFound = false;
                        continue;
                    } else if (firstStarFound == false
                            &amp;&amp; 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 &amp;&amp; closingStarFound == false) {
                    continue;
                }

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

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

                }

                if (slashCommentFound == false &amp;&amp; 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");
    }
}
</code></pre>

<p>And this code for listing directory tree:</p>

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

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

<p>The above code will work for java source code files but I'm not sure is it going to work for XML files, CSS files, Javascript and HTML files. I also need a help to write the code in a proper way.</p>

<p>Best wishes</p>
