針對一些很大的文本文件用工具打開編輯很慢的情況
public void readTxtFile(String filePath) {
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader br = new BufferedReader(isr);
String lineTxt = null;
FileWriter fw = new FileWriter("C:\\Users\\wangr\\Desktop\\深圳cntf數據\\test.csv",true);
while ((lineTxt = br.readLine()) != null) {
System.out.println(lineTxt);
/*
* 調用Writer對象中的write(string)方法,寫入數據。
* 其實數據寫入到臨時存儲緩衝區中。
*/
fw.write((lineTxt.replace("\"", ""))+"\r\n");//LINE_SEPARATOR是換行符
}
//進行刷新,將數據直接寫到目的地中。 注意:flush可以用多次。
fw.flush();
//關閉流,關閉資源。在關閉前會先調用flush刷新緩衝中的數據到目的地。 注意:close可以用多次。
fw.close();
br.close();
} else {
System.out.println("文件不存在!");
}
} catch (Exception e) {
System.out.println("文件讀取錯誤!");
}
}