在我的程序中我想读取 PLSQL 文件
并删除以--
开头的注释
我将每个评论放在它自己的行中,这样我就可以删除该特定行(有时我将代码和评论放在同一行中,这就是我正在做的“\n--”)。
我将程序导出到 jar 文件,它在我的桌面上运行良好,但在另一台计算机(读取不同的 PLSQL 文件)中,即使我尝试,它也会给我 Java 堆空间错误
java -Xmx256m -jar myjar.jar
错误:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at ParserDB.ScriptNoComment(ParserDB.java:142)
at ParserDB.GetTheName(ParserDB.java:54)
at Rapport.SearchCcInDB(Rapport.java:189)
at Rapport.listDB(Rapport.java:77)
at Rapport.main(Rapport.java:472)
... 5 more
我的代码是:
public static String ScriptNoComment(String fileName){
String result = null ;
try{
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer strOut = new StringBuffer();
StringBuilder Out = new StringBuilder();
String strLine;
while ((strLine = br.readLine()) != null) {
if(strLine.contains("--")){
strLine = strLine.replaceAll("--","\n--");
}
strOut.append(strLine+"\n");
}
in.close();
//delete comment
String[] lines = strOut.toString().split("\\n");
for(String s: lines){
if(s.contains("--")){
s="";
}
Out.append(s+"\n");
}
result = Out.toString();
result = result.toUpperCase();
result = result.replaceAll("\"", "");
result = result.replaceAll("\\r\\n|\\r|\\n", " ");
result = result.replaceAll("\\s+", " ");
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
return result ;
}
有没有办法优化我的代码,提前致谢
编辑
1-)
我使用以下命令检查了另一台计算机中的堆大小:
java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
结果是:最小:16M,最大:256M 所以我应该在 java -jar 中粘贴:-Xmx512m 而不是 -Xms256m
2-) 我删除了(仅用于测试)stringbuilder和所有replaceAll,但仍然遇到相同的错误,因为我的文件太大。
所以我所做的是计算我正在读取的每个文件的行数,并尝试(取决于行数)仅读取前 50 行,并将我的方法仅应用于这 50 行
谢谢大家的回答
最佳答案
如果您有 java 8,您可以尝试使用此代码在处理行时对行进行内联编辑
public static String scriptNoComment(String fileName) {
Path filePath = Paths.get(fileName);
try (Stream<String> stream = Files.lines(filePath)) {
List<String> linesWithNoComments = new ArrayList<String>();
stream.forEach(line -> {
if (line.startsWith("--")) {
return;
}
String currentLine = line;
int commentStartIndex = line.indexOf("--");
if (commentStartIndex != -1) {
currentLine = line.substring(0, commentStartIndex);
}
currentLine = currentLine.toUpperCase();
currentLine = currentLine.replaceAll("\"", "");
currentLine = currentLine.replaceAll("\\r\\n|\\r|\\n", " ");
currentLine = currentLine.replaceAll("\\s+", " ").trim();
if (currentLine.isEmpty()) {
return;
}
linesWithNoComments.add(currentLine);
});
return String.join("\n", linesWithNoComments);
} catch (IOException e) {
e.printStackTrace(System.out);
return "";
}
}
如果 java 8 不是一个选项,那么您可以使用 Apache StringUtils::join和 FileUtils::LineIterator达到同样的结果。希望这能解决问题。
编辑
已关注 Nicolas Filotto建议我在一定数量的处理行之后添加写入文件(该数量是完全随机选择的)。我测试了这两种方法,第一种方法因文件大小接近堆大小而失败(字符串中行的连接与操作代码具有相同的问题)。对于第二种方法,我使用 2GB 文件进行了测试,执行 2 分钟后,输入文件旁边出现了 ${fileName}_noComments
文件。
public static int LINES_BATCH = 10000;
private static void scriptNoComment(String fileName) {
Path filePath = Paths.get(fileName);
try (Stream<String> stream = Files.lines(filePath); BufferedWriter fileOut = getFileOutWriter(fileName)) {
List<String> linesWithNoComments = new ArrayList<String>();
stream.forEach(line -> {
if (line.startsWith("--")) {
return;
}
String currentLine = line;
int commentStartIndex = line.indexOf("--");
if (commentStartIndex != -1) {
currentLine = line.substring(0, commentStartIndex);
}
currentLine = currentLine.toUpperCase();
currentLine = currentLine.replaceAll("\"", "");
currentLine = currentLine.replaceAll("\\r\\n|\\r|\\n", " ");
currentLine = currentLine.replaceAll("\\s+", " ").trim();
if (currentLine.isEmpty()) {
return;
}
linesWithNoComments.add(currentLine);
if (linesWithNoComments.size() >= LINES_BATCH) {
writeCurrentBatchToFile(fileOut, linesWithNoComments);
}
});
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
private static BufferedWriter getFileOutWriter(String fileName) {
BufferedWriter fileOut;
try {
fileOut = new BufferedWriter(new FileWriter(fileName + "_noComments", false));
return fileOut;
} catch (IOException e) {
throw new RuntimeException("Error while creating out writer", e);
}
}
private static void writeCurrentBatchToFile(BufferedWriter fileOut, List<String> linesWithNoComments) {
try {
for (String line : linesWithNoComments) {
fileOut.write(line + " ");
}
linesWithNoComments.clear();
} catch(IOException e) {
throw new RuntimeException("Unable to write lines to file", e);
}
}
关于追加到 stringBuilder 时发生 java 堆错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36715030/