[英]Using group of regular expressions in java
i'm writing a program which should change text file with regex. 我正在编写一个程序,应使用正则表达式更改文本文件。 I have a few commands, and they are working in eg NotePad++.
我有一些命令,它们在例如NotePad ++中工作。 I want now the same commands in java to make it faster.
我现在希望在Java中使用相同的命令使其速度更快。 Can it be caused by the way it saves and reads the file?
可能是由于其保存和读取文件的方式引起的吗?
private List<String> lines = new ArrayList<String>(10000000);
// read original file to an ArrayList
public String[] readOriginalFile(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while (((line = bufferedReader.readLine()) != null)){
lines.add(line);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
public void replaceWordsOne() {
ArrayList<String> lns = new ArrayList<String>(10000000);
for (String ln : lines) {
lns.add(ln.replaceAll("^(\\[.*?\\])\\s+(\\[.*?\\])\\s(\\[.*?\\]\\s){1,}(\\[.*?\\])", "\1\t\2\t\4\t"));
}
lines.clear();
lines = lns;
}
public void writeToNewFileOne(String FinalDirectory) throws IOException {
FileWriter writer = new FileWriter(FinalDirectory);
for (String str : lines) {
writer.write(str +"\n");
}
writer.close();
}
i think that the main cause is this line: 我认为主要原因是这条线:
private List<String> lines = new ArrayList<String>(10000000);
in java arraylist are dynamic resize arrays, that's means that you don't need to initialize the list with elements. java arraylist中的是动态调整大小的数组,这意味着您不需要使用元素初始化列表。
should be like this: 应该是这样的:
private List<String> lines = new ArrayList<String>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.