字节流20200922

1,字节流,(1)读取纯文本文件优先使用字符流;(2)图片、音频优先使用字节流;(3)不确定是啥,就使用字节流。
字节就是byte,字符就是“A”,“@”这种,不同的编码方式占用不同的字节。
2,FIleOutputStreat(String s),可以用来写入数据。在IO操作时可以使用.write(。。。)写入数据;在IO操作之后要使用.close()方法来释放系统资源。
3,字节流写数据实现换行,在写完数据之后再写一句换行符号,window是“\r\n”,linux是“\n”,mac是“\r”,

    public static void main(String[] args) throws IOException {
        FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt");
        for(int i=0;i<10;i++){
            fos.write("hello".getBytes());
            fos.write("\r\n".getBytes());
        }
        fos.close();
    }
}

3,字节流写入数据追加写入,在创建对象时使用构造方法,

FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt",true);

就是以后写文件时在末尾追加写入。
4,字节流写数据的异常处理,
(1)

        try{
//            IO流
//            执行语句
        }catch (/*异常类 变量名,比如:IOException ioException*/ ){
//            catch的执行语句,异常处理代码
        }finally {
//            执行所有清除操作
        }

(2),通过alt+enter选择使用throws抛出异常。

public static void main(String[] args) throws IOException {
    FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt",true);
    for(int i=0;i<10;i++){
        fos.write("love".getBytes());
        fos.write("\r\n".getBytes());
    }
    fos.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值