我需要從文本文件中讀取所有「,」替換爲「\ n」,並將結果寫入新的文本文件。我的文本文件很大(16MB),並且需要幾個小時才能完成此任務。有沒有其他方式可以更快地實現它?運行時優化
public static void main(String [] args) throws IOException {
String fileName = "file1.txt"; // The name of the file to open.
String s=""; // This will reference one line at a time
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
int c, n=0;
char character;
while((c = reader.read()) != -1) {
character = (char) c;
if (character == ',')
s = s + "\n";
else
s += character;
}
reader.close();
PrintWriter out = new PrintWriter("fileName1.txt");
out.println(s);
out.close();
}
你是什麼意思?如果有一種方法不使用String,那會更好。我如何從輸出文件讀取,將「,」更改爲「/ n」,並直接寫入字符? – Ryainad
謝謝!它現在正在工作!以光速! – Ryainad