栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java io流详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java io流详解

一、IO流

IO流: 数据的读入写出,文件的上传下载
流: 一连串流动的数据,以先入先出的方式:
数据源---->io---->目的地
数据源 : 数据的来源
目的地 : 数据流向的位置

二、流的分类 1.操作单元分:

字节流 : 万能流 *****
字符流 : 纯文本内容

2.流向分: 以程序为中心

输入流
输出流

3.功能分:

节点流 : 真实能做读入写出的
功能流 : 增强节点流的功能,提高节点流的性能

三、流的应用 1.字节输入输出流 ①InputStream 字节输入流中最上层父类
    文件流 : FileInputStream 文件字节输入流->节点流  字节流  输入流  
         操作文件,数据源为文件,使用文件字节输入流
    字节数组流 : ByteArrayInputStream 字节数组输入流 ->节点流  字节流  输入流
        操作字节数组,数据源是字节输入,使用字节数组输入流
public class Class001_IO {
    public static void main(String[] args) throws IOException {
        File src = new File("D://test.txt");
        //FileInputStream(File file) 通过打开与实际文件的连接来创建 FileInputStream ,该文件由文件系统中的 File对象 file命名。
        InputStream is = new FileInputStream(src);

        //读入 int read() 每次读入一个字节数,返回读入到的字节,读不到返回-1
        

        //循环读入
        

        //提高效率,每次读入一车(字节数组)的数据
        //byte[] car = new byte[1024];
        //int read(byte[]) 每次读入一个字节数组的数据,返回读入到字节数组中数据的个数,没有读到返回-1
        //int len = is.read(car);
        //System.out.println(new String(car,0,len));

        //当内容比较多,需要重复读入,每次读入一个字节数组中的数据
        

        //byte[] readAllBytes() 从输入流中读取所有剩余字节。
        byte[] arr = is.readAllBytes();
        System.out.println(new String(arr));

        //关闭
        is.close();
    }
}

②OutputStream 字节流输出流
    FileOutputStream 文件字节输出流 -> 节点流
        目的地为文件
    ByteArrayOutputStream 字节数组输出流 -> 节点流
        目的地为字节数组
public class Class002_IO {
    public static void main(String[] args) throws IOException {
        //1.构建流,指定目的地
        //FileOutputStream(String name)
        //OutputStream os = new FileOutputStream("D://test.txt");  //默认覆盖原文件中内容
        OutputStream os = new FileOutputStream("D://BBB/test2.txt",true);  //boolean参数->true为追加  false为覆盖

        //2.写出
        os.write(97);

        byte[] arr = "你好再见".getBytes();

        os.write(arr);

        //3.刷出
        os.flush();

        //4.关闭
        os.close();
    }
}```

#### 字节流实现拷贝****

```java
public class Class001_CopyFile {
    public static void main(String[] args) throws IOException {
        //1.构建流(输入,输出)
        Reader rd = new FileReader("D://test.txt");
        Writer rt = new FileWriter("D://dest.txt");
        //2.读写
        //读入 int read() 每次读入一个字符数据
        //读入 int read(char[] cbuf) 将字符读入数组。返回读取的字符数,如果已到达流的末尾,则返回-1
        char[] car = new char[1024];
        int len = -1; //记录每次读入到数组中数据的个数
        while((len=rd.read(car))!=-1){
            //void write(char[] cbuf) 写一个字符数组。
            //void write(char[] cbuf, int off, int len) 写一个字符数组的一部分。
            //void write(int c) 写一个字符。
            //void write(String str) 写一个字符串。
            //void write(String str, int off, int len) 写一个字符串的一部分。
            rt.write(car,0,len);
        }
        //3.刷出
        rt.flush();
        //4.关闭
        rt.close();
        rd.close();
    }
}

数据源–>输入流–>程序–>输出流–>目的地

步骤:
    1.构建流(输入流  输出流)
    2.读入写出
    3.刷出
    4.关闭 (后打开的先关闭)
2.字符流输入输出
 Reader  字符输入流
        FileReader 文件字符输入流  ->节点流
 Writer  字符输出流
        FileWriter 文件字符输出流  ->节点流
3.功能流 ①缓冲流
    缓冲流 Buffered : 加快节点流的读写效率
        字节缓冲流 :
            字节输入缓冲流 BufferedInputStream
            字节输出缓冲流 BufferedOutputStream
                无新增方法,可以发生多态
        字符流缓冲流
    		字符输入缓冲流 BufferedReader
       		    新增功能: String readLine() 读一行文字。
   	    字符输出缓冲流 BufferedWriter
               新增功能: void newline() 写一个行分隔符。

字节缓冲流

public class Class001_BufferedInputStream {
    public static void main(String[] args) throws IOException {
        //1.构建流
        InputStream is = new BufferedInputStream(new FileInputStream("D://test.txt"));
        OutputStream os = new BufferedOutputStream(new FileOutputStream("D://test1.txt"));

        //2.读入写出
        byte[] car = new byte[1024];
        int len = -1;
        while((len = is.read(car))!=-1){
            os.write(car,0,len);
        }

        //3.刷出
        os.flush();

        //4.关闭
        os.close();
        is.close();
    }
}

字符缓冲流(存在新增功能,不能发生多态)

public class Class002_BufferedReader {
    public static void main(String[] args) throws IOException {
        //1.构建流(输入,输出)
        BufferedReader rd = new BufferedReader(new FileReader("D://test.txt"));
        BufferedWriter rt = new BufferedWriter(new FileWriter("D://dest.txt"));
        //2.读写
        String msg = null;
        while((msg=rd.readLine())!=null){
            rt.write(msg);
            //rt.newline(); //换行符
        }
        //3.刷出
        rt.flush();
        //4.关闭
        rt.close();
        rd.close();
    }
}

②Date流(可以读写基本数据类型、字符串,不能读写对象类型等)
是字节流的功能流
        DataInputStream     Data输入流
            新增功能 : readXxx()
        DataOutputStream   Data输出流
            新增功能 : writeXxx()
public class Class003_DataInputStream {
    public static void main(String[] args) throws IOException {
        read("D://test1.txt");
    }

    //写出
    public static void wirte(String path) throws IOException {
        //1.构建输出流
        DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
        //2.准备数据
        int i = 100;
        char ch = 'a';
        boolean flag = false;
        String str = "哈哈";

        //3.写出
        os.writeInt(i);
        os.writeChar(ch);
        os.writeBoolean(flag);
        os.writeUTF(str);

        //4.刷出
        os.flush();

        //5.关闭
        os.close();
    }


    //读入
    public static void read(String path) throws IOException {
        //1.构建输入流
        DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));

        //2.读入
        int i = is.readInt();
        char ch = is.readChar();
        boolean flag = is.readBoolean();
        String msg = is.readUTF();
        //3.处理数据
        System.out.println(i);
        System.out.println(ch);
        System.out.println(flag);
        System.out.println(msg);

        //4.关闭
        is.close();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号