OutputStream 字节输出基类

OutputStream 字节输出基类

写操作步骤

步骤1:打开字节输出流,可能会抛出FileNotFoundException异常
String path = “/Users/gongdezhe/Desktop/test.txt”;
FileOutputStream outputStream = new FileOutputStream(path);
写操作时,文件不存在,会自动创建文件,并写入数据
当文件目录路径不合法是则会抛出异常
步骤2:写操作,可能会抛出IOException异常
void write(int b) 每次写入一个字节
步骤3:关闭流,可能会抛出IOException异常
outputStream.close();
方法介绍:
void write(int b) :
每次写入一个字节
void write(byte b[]) ;
批量写入,写入类型为byte[]
void write(byte b[], int off, int len)
批量写入,可以自定义从byte数组的起始和读取长度
void flush()
刷新缓存

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

   public static void Write()  {
   //写操作介绍
   //步骤1:打开字节输出流,可能会抛出FileNotFoundException异常
   String path = "C:\\Users\\lenovo\\Desktop\\a.txt";
   //写操作时,文件不存在,会自动创建文件,并写入数据
   //当文件目录路径不合法时则会抛出异常
   try{
      FileOutputStream outputStream = new FileOutputStream(path);
      //步骤2:写操作,可能会抛出IOException异常
            //void write(int b);  每次写入一个
            // outputStream.write(99);
            // void write(byte b[])  批量写入,写入类型为byte[]
            byte[] bytes = {'a','b','c'};
//           outputStream.write(bytes);//abc写入
//           outputStream.write("hello".getBytes());
           //void write(byte[] b,int off,int len);  //批量写入,可以自定义从byte数组的起始和读取长度
            outputStream.write(bytes,0,2); //[1,2)   必须在byte[]数组的长度的有效范围内
            outputStream.getFD(); //通道相关
            outputStream.getChannel();//文件
            outputStream.flush(); //提醒磁盘直接发送文件,刷新缓存,刷新内核区
           //步骤3:关闭流,可能会抛出IOException异常
           outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值