i'm writing a program which should change text file with regex. I have a few commands, and they are working in eg NotePad++. I want now the same commands in java to make it faster. 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.
should be like this:
private List<String> lines = new ArrayList<String>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.