FileOutputStream类的使用

java.io.OutPutStream;

/*

void write(byte[] b)将 b.length 个字节从指定 byte 数组写入此文件输出流中。
 void write(byte[] b, int off, int len)将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
 void write(int b)将指定字节写入此文件输出流。

字节输出流的使用步骤
    1、创建FileOutputStream对象,构造方法中写入数据的目的地,
    2、调用FileOutputStream对象中的方法Write,把数据写入到文件中,
    3、释放资源

*/

private static void demo04() throws IOException {
        FileOutputStream fos=new FileOutputStream("IOAndProperties\\1.txt",true);
        fos.write(97);
        fos.write(65);
        fos.write(48);
        System.out.println("=================================");
        byte[] b={'a','f',32,33,34,'h'};
        fos.write(b);
        fos.write(b,2,3);
    }

    /*
    void write(byte[] b, int off, int len)将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
     */
    private static void demo03() throws IOException {
        //创建fos对象,//后边添加了true,表示
        FileOutputStream fos = new FileOutputStream("C:\\Users\\DCF\\Desktop\\a.txt",true);
        //调用对象中的方法write,把数据写入到文件中
        byte[] b={48,49,50,65,66,97,98};
        fos.write(b,1,2);//指定偏移量的写入数据,
        fos.write("\r\n".getBytes());//写入换行字符
        fos.write("您好".getBytes());//写入字节数组
        for (int i = 0; i < 10; i++) {
            fos.write("你好".getBytes());
            fos.write("\r\n".getBytes());
        }
        //释放资源
        fos.close();
        System.out.println("写入成功;");
//        int a=0;
//        boolean b1 = (a = 10) != -1;
//        System.out.println(a=10);
    }

    /*
       void write(byte[] b)将 b.length 个字节从指定 byte 数组写入此文件输出流中。
     */
    private static void demo02() throws IOException {
        //创建fos对象,
        FileOutputStream fos = new FileOutputStream("C:\\Users\\DCF\\Desktop\\1.txt");
        //调用对象中的方法write,把数据写入到文件中
        byte[] b={48,49,50,65,66,97,98};
        fos.write(b);
        //释放资源
        fos.close();
        System.out.println("写入成功;");
    }

    /*
      void write(int b)将指定字节写入此文件输出流。
     */
    private static void demo01()throws IOException  {
        //创建fos对象,
        FileOutputStream fos = new FileOutputStream("C:\\Users\\DCF\\Desktop\\1.txt");
        //调用对象中的方法write,把数据写入到文件中
        fos.write(48);
        //释放资源
        fos.close();
        System.out.println("写入成功;");
    }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值