Java学习之路---高级编程(IO流概述,File的基本使用,字节流,字节缓冲流的基本使用)

IO流:

IO流的简单概述

使用完io流需要释放资源!!!
在这里插入图片描述

IO流的分类:

在这里插入图片描述
一般视频或者图片使用字符流,txt文本文件等,使用字节流

字节流:

在这里插入图片描述

字节流基类:

在这里插入图片描述

字节流写数据:

FileOutputStream:

步骤:

在这里插入图片描述
在这里插入图片描述

写数据三种方式:

在这里插入图片描述

示例:
import java.io.FileOutputStream;
import java.io.IOException;

public class Test01 {
    public static void main(String[] args) throws IOException {
        //没有此文件自动创建,有此文件会覆盖原文件
        FileOutputStream fos=new FileOutputStream("fos.txt");
        
        fos.write(97);
        fos.write(57);
        /*
        也可以使用getBytes方法直接将字符串转换为字节数组写入
        String a="abcd";
        byte[] bytes = a.getBytes(StandardCharsets.UTF_8);
        fos.write(bytes);        
        */
        
        //释放资源
		fos.close();
    }
}

在这里插入图片描述
写数据是按照ASCLL码来写的

写入换行和空格:

在这里插入图片描述

FileInputStream:

常用方法:

读数据使用

示例:

一次读取一个字节:

import java.io.FileInputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("fos.txt");

        int read = fis.read();
        System.out.println(read);
        System.out.println((char)read);
        int read1 = fis.read();
        System.out.println(read1);
        System.out.println((char)read1);
		fis.close();

    }
}

在这里插入图片描述
在read()读到最后时,读取到的时-1。
使用循环读取文件:

import java.io.FileInputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("fos.txt");
        int read;
        while ((read=fis.read())!=-1) {
            System.out.print((char)read);
        }
        fis.close();
    }
}

在这里插入图片描述

案例:读取数据在控制台输出

文本文件:
在这里插入图片描述

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis=new FileInputStream("fos.txt");
        //设置读取的空间
        byte b[]=new byte[1024];
        
        //使用循环结构,读取数据
        while (fis.read(b)!=-1){
            System.out.print(new String(b));
        }

        fis.close();
    }
}

输出结果:
在这里插入图片描述

字节缓冲流:

字节缓冲流的概述

在这里插入图片描述
在构造方法时,不是具体文件路径,是因为字节缓冲流仅仅提供缓冲区,真正读写数据还得靠字节流对象来操作。
在源码中,字节缓冲流创建对象时,可以看到默认有个初始值为8192,代表默认定义的缓冲空间
在这里插入图片描述

BufferedInputStream:

示例:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("fos.txt"));
        byte b[]=new byte[1024];
        while (bis.read(b)!=-1){
            System.out.print(new String(b,0,b.length));
        }

        bis.close();
    }
}

在这里插入图片描述

BufferedOutputStream

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        //true 代表追加写入
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("fos.txt",true));
        bos.write("hello".getBytes());
        bos.close();

    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

牛总来学习了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值