IO流之字节流写数据详解

1、字节流写数据的三种方式一:

// 字节流写数据的三种方式一:
public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        // 创建字节输出流对象
        FileOutputStream fos = new FileOutputStream("D:\\data\\fos.txt");

        // 调用系统功能创建了文件
        // 创建了字节输出流对象
        // 让字节输出流对象指向创建好的文件
        //
        // void write(int b)
        // 字节流写数据的三种方式一:将指定的字节写入此文件输出流 一次写一个字节数据
        fos.write(97);//a

        // 最后都要释放资源
        // 关闭此文件输出流并释放与此流相关联的任何系统资源
        fos.close();
    }
}

2、字节流写数据的三种方式二:

//字节流写数据的三种方式二
public class FileOutputStreamDemo02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\data\\fos.txt");
        byte[] bys = "abcde".getBytes();
        //字节流写数据的三种方式二:将 b.length字节从指定的字节数组写入此文件输出流   一次写一个字节数组数据
        fos.write(bys);
        // 释放资源
        fos.close();
    }
}

3、字节流写数据的三种方式三:

//字节流写数据的三种方式三:
public class FileOutputStreamDemo03 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\data\\fos.txt");
        byte[] bys = "abcde".getBytes();
        //字节流写数据的三种方式三:将 b.length字节从指定的字节数组写入此文件输出流   一次写一个字节数组数据
        fos.write(bys,1,2);
        // 释放资源
        fos.close();
    }
}

4、字节流写数据如何实现换行、字节流写数据如何实现追加写入

//字节流写数据如何实现换行、字节流写数据如何实现追加写入
public class FileOutputStreamDemo04 {
    public static void main(String[] args) throws IOException {
//        - 字节流写数据如何实现换行
//
//                - windows:\r\n
//                - linux:\n
//                - mac:\r
//
//        - 字节流写数据如何实现追加写入
//
//                - public FileOutputStream(String name,boolean append)
//        - 创建文件输出流以指定的名称写入文件。如果第二个参数为true ,则字节将写入文件的末尾而不是开头
        FileOutputStream fos = new FileOutputStream("D:\\data\\fos.txt", true);
        // 写数据
        for (int i = 0; i < 10; i++) {
            fos.write("hello".getBytes());
            fos.write("\r\n".getBytes());

        }
        // 释放资源
        fos.close();
    }
}

5、字节流写数据加异常处理

//字节流写数据加异常处理
public class FileOutputStreamDemo05 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\data\\fos.txt");
            fos.write("hello".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加入finally来实现释放资源
        finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

91科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值