我有.sh,.txt,.sql,.pkb等文件,文件大小超過10 MB,這意味着超過10萬行。使用Java從大文件中刪除註釋
我想從這些文件中刪除註釋,然後再使用未註釋的內容。我爲它編寫了下面的代碼。
/**
* Removes all the commented part from the file content as well as returns a
* file structure which have just lines with declaration syntax for eg.
* Create Package packageName <- Stores all decalartion lines as separate
* string in an array
*
* @param file
* @return file content
* @throws IOException
*/
private static String[] filterContent(File file) throws IOException {
String withoutComment = "";
String declare = "";
String[] content;
List<String> readLines = FileUtils.readLines(file);
int size = readLines.size();
System.out.println(file.getName() + " Files number of lines "+ size + " at "+new Date());
String[] declareLines = new String[size];
int startComment = 0;
int endComment = 0;
Boolean check = false;
int j = 0;
int i=0;
// Reading content line by line
for (String line:readLines) {
// If line contains */ that means comment is ending in this line,
// making a note of the line number
if (line.toString().contains("*/")) {
endComment = i;
// Removing the content before */ from the line
int indexOf = line.indexOf("*/");
line = line.replace(line.substring(0, indexOf + 2), "");
}
// If startComment is assigned fresh value and end comment hasn't,
// that means the current line is part of the comment
// Ignoring the line in this case and moving on to the next one
if ((startComment > 0 && endComment == 0) || (endComment < startComment) || check)
continue;
// If line contains /* that means comment is starting in this line,
// making a note of the line number
if (line.contains("/*")) {
startComment = i;
// Removing the content after /* from the line
int indexOf = line.indexOf("/*");
line = line.replace(line.substring(indexOf), "");
if (i == 0)
check = true; // means comment in the very first line
}
// If line contains -- that means single line comment is present in
// this line,
// removing the content after --
if (line.contains("--")) {
int indexOf = line.indexOf("--");
line = line.replace(line.substring(indexOf), "");
}
// If line contains -- that means single line comment is present in
// this line,
// removing the content after --
if (line.contains("#")) {
int indexOf = line.indexOf("#");
line = line.replace(line.substring(indexOf), "");
}
// At this point, all commented part is removed from the line, hence
// appending it to the final content
if (!line.isEmpty())
withoutComment = withoutComment + line + " \n";
// If line contains CREATE its a declaration line, holding it
// separately in the array
if (line.toUpperCase().contains(("CREATE"))) {
// If next line does not contains Create and the current line is
// the not the last line,
// then considering two consecutive lines as declaration line,
if (i < size - 1 && !readLines.get(i + 1).toString().toUpperCase().contains(("CREATE"))) {
declare = line + " " + readLines.get(i + 1).toString() + "\n";
} else if (i < size) {// If the line is last line, including
// that line alone.
declare = line + "\n";
}
declareLines[j] = declare.toUpperCase();
j++;
}
i++;
}
System.out.println("Read lines "+ new Date());
List<String> list = new ArrayList<String>(Arrays.asList(declareLines));
list.removeAll(Collections.singleton(null));
content = list.toArray(new String[list.size() + 1]);
withoutComment = withoutComment.toUpperCase();
content[j] = withoutComment;
System.out.println("Retruning uncommented content "+ new Date());
return content;
}
public static void main(String[] args) {
String[] content = filterContent(new File("abc.txt"));
}
這個代碼的問題是它太慢,如果文件大小很大。對於10 MB文件,刪除評論需要6個多小時。 (代碼在SSH服務器上運行)。
我可以擁有大小不超過100 MB的文件,在這個文件中需要幾天時間才能刪除評論。我如何更快地刪除評論?
更新:問題不是重複的,因爲我的問題不僅僅是通過改變閱讀行的方式來解決。它的字符串活動使得這個過程變得緩慢,我需要一種方法來使評論移除活動更快。
1.不要將整個文件放在內存中。 2.你爲什麼想這樣做? – Axel
首先,不要把它放到列表中,使用InputStream讀取文件並直接分析行。你可以很容易地找到一行是否包含'/ *'或'/ * ... * /',刪除它並重新創建沒有註釋的新文件。讀取超過100MB的文件應該不會花費那麼長的時間... – AxelH
[如何使用Java逐行讀取大型文本文件?](http://stackoverflow.com/questions/5868369/how-to -read-a-large-text-file-line-by-java) – AxelH