私は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 ファイルに対して機能するかどうかはわかりません。また、適切な方法でコードを書くための助けも必要です。
幸運をお祈りしています