BufferedReader和BufferedWriter
时间: 2023-12-12 13:10:38 浏览: 130
BufferedReader 和BufferedWriter 实例
BufferedReader和BufferedWriter是Java中的两个I/O类,用于高效地读写数据流。BufferedReader提供了缓存机制来读取字符流,可以一次读取多个字符并且减少I/O操作的次数,提高了效率。BufferedWriter同样提供了缓存机制来写入字符流,可以一次写入多个字符并且减少了I/O操作的次数,提高了效率。
用法示例:
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
String line = reader.readLine();
while (line != null) {
// 处理读取到的行数据
line = reader.readLine();
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
writer.write("Hello, world!");
writer.newLine(); // 写入换行符
writer.write("This is a test.");
writer.close();
阅读全文