Java -- Buffered

本文介绍如何使用Java中的缓冲流进行图片文件的复制,并演示了利用缓冲读写器进行文本文件的内容复制与处理的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class TestBuffered {
public static void main(String[] args) {
//使用缓冲流实现费文本文件的复制
FileInputStream f=null;
FileOutputStream f2=null;
try {
//提供读入,写出的文件
File file=new File("hello.jpg");
File file2=new File("hello2.jpg");
//创建相应的节点流
f = new FileInputStream("file");
f2 = new FileOutputStream("file2");
//将创建的节点流的对象作为形参传递给缓冲流的构造器
BufferedInputStream inputStream = new BufferedInputStream(f);
BufferedOutputStream outputStream = new BufferedOutputStream(f2);
//实现文件的复制
byte[] b=new byte[1024];
int len;
while((len=inputStream.read(b))!=-1){
outputStream.write(b,0,len);
outputStream.flush();//刷新
}
} catch (IOException e) {
e.printStackTrace();
//关闭流
}finally{
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
test();
}


private static void test() {
BufferedReader br=null;
BufferedWriter bw=null;
try {
File file=new File("hello.txt");
File file2=new File("hello2.txt");
FileReader fr=new FileReader(file);
FileWriter fw = new FileWriter(file2);
br=new BufferedReader(fr);

// char[] c=new char[1024];
// int len;
// while((len=br.read(c))!=-1){
// String s=new String(c,0,len);
// System.out.println(s);
// }
String str=null;
while((str=br.readLine())!=null){
// System.out.println(str);
bw.write(str);
bw.newLine();//换行
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){

try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}
### Java 中 `PrintReader` 和 `BufferedReader` 的使用区别 在讨论 `PrintReader` 和 `BufferedReader` 之前,值得注意的是,在标准的 Java API 文档中并不存在名为 `PrintReader` 的类。可能存在混淆的情况是指 `PrintWriter` 或者其他自定义实现。 #### PrintWriter 特性 `PrintWriter` 是一个用于高效打印格式化表示形式的输出流工具。它提供了方便的方法来处理不同类型的输出操作,比如字符串、基本数据类型等,并且可以自动刷新缓冲区[^3]。 ```java try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")))) { out.println("This is a line of text."); } catch (IOException e) { e.printStackTrace(); } ``` #### BufferedReader 特性 `BufferedReader` 主要针对高效的读取字符输入流而设计,特别是当需要逐行读取大文件时非常有用。其内部维护了一个字符缓冲区以减少底层 I/O 操作次数,从而提高效率[^2]。 ```java try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } ``` ### 性能对比 对于性能方面: - **内存占用**:由于 `BufferedReader` 维护了一定大小的缓存空间,所以在大量连续的数据读入场景下能够显著降低磁盘访问频率;相比之下,如果只是简单的写入少量数据到文件,则 `PrintWriter` 可能在某些情况下显得更加轻量级。 - **速度表现**:通常来说,在批量处理文本内容(尤其是按照行的方式)的时候,`BufferedReader` 表现得更快一些因为它减少了频繁调用系统级别的 IO 函数所带来的开销[^1]。 综上所述,选择哪一个取决于具体的应用需求——如果你的任务涉及到大量的文本解析工作,那么推荐优先考虑 `BufferedReader`; 而当你想要简化代码逻辑并且不需要特别关注执行时间的话,`PrintWriter` 则是一个不错的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值