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();
}
}
Java读写文本文件。
最新推荐文章于 2023-08-17 16:01:13 发布