72- IO 字节输出流基本用法

OutputStream:

  • FileOutputStream:将字节数据写入文件的输出流。
  • ByteArrayOutputStream:将字节数据写入字节数组的输出流。
  • ObjectOutputStream:将Java对象写入输出流的输出流。

一.FileOutputStream 使用步骤:

FileOutputStream 是 Java IO 中用于将字节数据写入文件的输出流类。它继承自 OutputStream 类。

1.创建 FileOutputStream 对象:可以通过提供文件路径或 File 对象来创建 FileOutputStream 对象。

FileOutputStream fos = new FileOutputStream("file.txt"); // 通过文件路径创建
// 或者
File file = new File("file.txt");
FileOutputStream fos = new FileOutputStream(file); // 通过 File 对象创建

2.写入字节数据:可以使用 write() 方法将字节数据写入输出流。

byte[] data = "Hello, World!".getBytes(); // 要写入的字节数据
fos.write(data); // 将字节数据写入输出流

还有其他重载版本的 write() 方法可用于写入部分字节数组或单个字节。

3.刷新缓冲区(可选):可以使用 flush() 方法刷新输出流的缓冲区,确保数据被立即写入文件。

fos.flush(); // 刷新缓冲区,确保数据被写入文件

4.关闭输出流:在不再需要输出流时,应该关闭它以释放系统资源。

fos.close(); // 关闭输出流

最佳实践是使用 try-with-resources 语句块来自动地关闭输出流,确保在异常情况下也能正确关闭。

try (FileOutputStream fos = new FileOutputStream("file.txt")) {
    // 写入操作
} catch (IOException e) {
    // 异常处理
}

二、FileOutputStream 写数据

1.写数据的3种方式

方式一:逐字节写入:这是最基本的写入方式,逐个字节地将数据写入输出流。
try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    byte[] data = {65, 66, 67};  // 示例数据
    for (byte b : data) {
        fos.write(b);
    }
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
方式二:写入字节数组:通过将字节数组作为整体写入,可以提高写入效率。
try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    byte[] data = {65, 66, 67};  // 示例数据
    fos.write(data);
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
方式三:使用缓冲区写入:使用缓冲区来进行写入操作,可以在一定程度上提高性能。
try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] data = {65, 66, 67};  // 示例数据
    bos.write(data);
    bos.close();
} catch (IOException e) {
    e.printStackTrace();
}

2.换行写数据:

  • 方式一:逐字节写入并手动添加换行符
try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    byte[] data = {65, 66, 67};  // 示例数据
    for (byte b : data) {
        fos.write(b);
    }
    fos.write('\n');  // 手动添加换行符
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
  • 方式二:将字符串转换为字节数组,并在末尾添加换行符
try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    String str = "Hello, World!";
    byte[] data = (str + "\n").getBytes();  // 将字符串转换为字节数组并在末尾添加换行符
    fos.write(data);
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
  •  方式三:使用 BufferedWriter 进行写入并调用 newLine() 方法

try {
    FileOutputStream fos = new FileOutputStream("file.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    BufferedWriter bw = new BufferedWriter(osw);
    String str = "Hello, World!";
    bw.write(str);
    bw.newLine();  // 使用 newLine() 方法添加换行符
    bw.close();
} catch (IOException e) {
    e.printStackTrace();
}

3.续写数据:要在已有文件的末尾处续写数据,可以使用 FileOutputStream 的构造方法中传入第二个参数为 true,这将打开文件以进行追加操作。

try {
    FileOutputStream fos = new FileOutputStream("file.txt", true);  // 打开文件以进行追加操作
    String str = "This is new data.";
    byte[] data = str.getBytes();  
    fos.write(data);  // 追加写入数据
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值