输入流输出流

流的分类

流就相当于快递员 , 将储存在各个地方的数据送到内存中 ( 或者相反 ) , 流是一种资源 , 使用完之后需要释放

​ 流 根据一次操作的数据大小不同分为字节流和字符流 , 字节流适合无损读取 , 音频 , 视频 之类的 , 字符流适合文本

流类型 字节流 字符流
输入流 InputStream Reader
输出流 OutStream Writer

​ 注意 , 这四个类都是抽象类 , 我们将会学习使用这些抽象类的实现类

字节流操作

常见字节流

字节输入流

image-20220821151237175
流对象
File InputStream 文件输入流
Buffered InputStream 缓冲字节输入流//可以操作文件
File InputStream 对象字节输入流

字节输出流

image-20220902165717745

输入流详解

文件输入流

流的创建

流就相当于运送数据的快递员 , 创建一个流对象 , 就要让他认识他要给谁送快递

通过一个实际的文件

File file = new File("d:\\hello.txt");
try{
    FileInputStream fileInputStream = new FileInputStream(file);
}

通过一个文件的绝对路径

FileInputStream fileInputStream = new FileInputStream("d:\\hello.txt");

通过一个文件的描述符

流的关闭

流相当于资源 , 如果不关闭会浪费资源

 finally {
    try {
        fileInputStream.close();
    } catch (IOException E) {
    }
}

流的输入

read( )
read( )
入参
返回值 返回读取的字符 , 可以用int接收 , 到达文件末尾 , 返回 -1
注释 不适合汉字读取 , 会出现乱码
public void read01() {
    File file = new File("d:\\hello.txt");
    FileInputStream fileInputStream = null;//要把流放在创建的外面,不然关闭流的finally代码无法访问创建在try中的流资源
    int readData = 0;//读取的是字节,转化成int
    try {
        fileInputStream = new FileInputStream(file);//在try中创建的资源
        while ((readData = fileInputStream.read()) != -1) {//read读取
            System.out.print((char) readData);//如果文件里面有汉字,一个汉字三个字节表示,所以会出现乱码
        }
    } catch (IOException e) {
    } finally {//最后要释放流资源
        try {
            fileInputStream.close();
        } catch (IOException E) {
        }
    }
}

read( byte [ n ] )

按字节数组读取

read( byte ( n ))
入参 希望一次性读入的字节数 , 一般是2的整数
返回值 用 int 接收 , 返回实际读取的字节数 ,读到结尾返回 -1 , 数据保存在 byte ( n ) 中
注释 比read( )效率高 , 读取内容转化成字符串需要用 new String ( byte ( n ) , 0 , readlenth)
@Test
public void read02() {
    File file = new File("d:\\hello.txt");
    FileInputStream fileInputStream = null;
    byte[] a = new byte[8];
    int readlenth = 0;//记录实际读取字节数
    try {
        fileInputStream = new FileInputStream(file);
        while ((readlenth = fileInputStream.read(a)) != -1) {//一次读8个,读到a数组中
            System.out.print(new String(a,0,readlenth));//使用a数组创建字符串
        }
    } catch (IOException e) {
    } finally {
        try {
            fileInputStream.close();
        } catch (IOException E) {
        }
    }

}

输出流详解

文件输出流

输入方法
leOutputStream.write('a') 写入单个字符
fileOutputStream.write(a.getBytes()) 写入字符数组
fileOutputStream.write(a.getBytes(), 0, a.length()) 写入指定长度的字符数组
leOutputStream.write(12) 写入int对应的ACS码值

如果文件不存在 , 会自己创建文件 , 前提是目录存在

public void out01() {
    File file = new File("d:\\hello.txt");
    FileOutputStream fileOutputStream = null;//写在外面解决关闭的问题
    try {
        fileOutputStream = new FileOutputStream(file,true);//如果没有TRUE,则是在每一次运行程序的时候从开头开始写,之前的会被覆盖掉,注意,如果是在一次程序中多次使用流输出不会有覆盖问题,多次使用程序才会出现这样的问题
        fileOutputStream.write('a');//第一种,单个字符
        String a = "赤苇";//将字符串转化成字符数组
        fileOutputStream.write(a.getBytes());//第二种,字符数组
        fileOutputStream.write(a.getBytes(), 0, a.length());//第三种,指定字符数组的长度输入
    } catch (IOException e) {
        e.printStackTrace();
    }
}

文件拷贝

public void cope() {
    /*
    1.边读边写
    2.一个输入流把数据读到内存,一个输出流把数据复制到另外一个文件
     */
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    File file = new File("d:\\cs.png");
    File file1 = new File("d:\\cs1.png");
    try {
        fileInputStream = new FileInputStream(file);
        fileOutputStream = new FileOutputStream(file1);
        int writeLenth = 0;
        byte[] a = new byte[1024];//一块一块的读写
        while ((writeLenth = fileInputStream.read(a)) != -1) {
            fileOutputStream.write(a, 0, writeLenth);//读到了就写出去
        }
    } catch (IOException E) {
        E.printStackTrace();
    } finally {
        try {
            if (fileInputStream!=null){//
                fileInputStream.close();
            }
            if (fileOutputStream!=null){
                fileOutputStream.close();
            }
        } catch (IOException e) {

        }
    }
}

字符流操作

常见字符流

字符输入流

字符输出流

字符输入流详解

流的创建

​ 通过地址 String 或者 文件 File

File file = new File("d:\\hello.txt");
fileReader = new FileReader(file);//通过文件创建输入流
fileWriter = new FileWriter("d:\\aks.txt", true);//通过路径创建输出流

流的输入

使用方法循环读取

方法名
fileReader.read() 返回单个char , 可以用int接受 , 读完返回-1
fileReader.read(datas) 返回读取数组长度 , 数据由datas接受 , 读完返回-1 , 使用 new String(datas, 0, lenth)转化成字符串输出
public void reader01() {
    FileReader fileReader = null;
    File file = new File("d:\\hello.txt");
    int lenth = 0;
    try {
        fileReader = new FileReader(file);
        char[] datas = new char[8];
        while ((lenth = fileReader.read()) != -1) {//第一个方法
            System.out.print((char) lenth);
        }
        while ((lenth = fileReader.read(datas)) != -1) {//第二个方法
            System.out.println(new String(datas, 0, lenth));
        }
    } catch (IOException e) {
    } finally {
        try {
            fileReader.close();
        } catch (IOException e) {

        }
    }
}

字符输出流详解

方法
fileWriter.write(127) 输出int ACS值 或者单个char
fileWriter.write(char[]); 输出char数组
fileWriter.write(char [ ] , 0 , chars.length( ) ) 输出指定长度的字符数组
fileWriter.write(String) 输出字符串
fileWriter.write(String , 0 , String.length( ) ) 输出指定长度的字符串

输出流的注意点

  1. 注意输出的时候是否需要覆盖 new FileWriter("d:\aks.txt", true)
  2. 注意最后要关闭流才算真正写入 fileWriter.close() close 中调用真正写入数据 writebyte 的方法
public void Writer01(){
    FileWriter fileWriter = null;
    char[] chars = new char[]{'赤','苇','!'};
    String cw = "赤苇我老婆,快和我贴!!";
    try {
    fileWriter = new FileWriter("d:\\aks.txt", true);//没有true则会覆盖
    fileWriter.write(127);//int,int是ACS值
    //单个字符也可以,write('a')
    fileWriter.write(chars);//char[]
    fileWriter.write(chars,0,chars.length);//char[],开始下标,截取长度
    fileWriter.write(cw);//String
    fileWriter.write(cw,0,cw.length());//String,开始下标,截取长度
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try {
            fileWriter.close();//不关闭或者不刷新则内容在内存里面,没有写到文件里面!!
            //close中才在真正写入数据writebyte的方法
        }catch (IOException e){

        }
    }
posted @   卓亦苇  阅读(12)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示