Java讀寫文本文件。

String path = "G:\\test.txt";

BufferedWriter write = null;
try {
	write = new BufferedWriter(new OutputStreamWriter(
			new FileOutputStream(path), "UTF-8"));

	for (int i = 0; i < 1000; i++) {
		write.append(String.valueOf(Math.random()));
		write.append(System.lineSeparator());
	}

	write.flush();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		write.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

BufferedReader reader = null;
try {
	reader = new BufferedReader(new InputStreamReader(
			new FileInputStream(path), "UTF-8"));
	String line;
	while ((line = reader.readLine()) != null) {
		System.out.print(line);
		System.out.print(System.lineSeparator());
	}
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		reader.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章