IO流输入输出流入门

目录

一、IO流

1.IO分类:

2.IO流类结构图:

二、输入、输出流

1.节点流

对管道进行操作:

2.内存流

3.缓冲流:(处理流)

Buffered缓冲流:

转化流:

数据流:

打印流:

对象流:

序列化流:


一、IO流


输入输出流: I input : O output

1.IO分类:

按照方向分: 输入流 和输出流

输入流: 读操作

输出流: 写操作

按照IO管道大小: 字节流: 一个字节一个字节的读写: 万能流

字符流: 一个字符一个字符读写: 读写文本文件

按功能分:

节点流:用于直接操作目标设备的流

处理流:是对一个已存在的流的连接和封装,通过对数据的处理为程序提供更强大、灵活的读写功能。

2.IO流类结构图:

4个抽象父类

字节输入流: InputStream

字节输出流: OutputStream

字符输入流: Reader

字符输出流: Writer
 

read() 读方法

close() 关闭

输出流主要方法

write() 写方法

close() 关闭

编写步骤:

  1. 创建IO流对象

  2. 读/写操作

  3. 关闭资源

二、输入、输出流

1.节点流

(1)字节节点流(万能流)

1.FileInputStream

FileInputStream fis  = null;
​
//1.构造方法
FileInputStream(File file) 
通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。  
FileInputStream(FileDescriptor fdObj) 
创建 FileInputStream通过使用文件描述符 fdObj ,其表示在文件系统中的现有连接到一个实际的文件。  
FileInputStream(String name) 
通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。  
//第一个和第三个构造方法差不多,第一个适合方法调用,第三个平时少写一行代码
​
​
//2.应用
        try {
            //1.创建IO流对象
            fis = new FileInputStream("d:/hello.txt");
            //2.进行读写操作
            int rs = -1;
            while((rs=fis.read()) != -1){
                System.out.println((char)rs);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

2.FileOutputStream

FileOutputStream fos  = null;
//1.构造方法
FileOutputStream(File file) 
创建文件输出流以写入由指定的 File对象表示的文件。  
FileOutputStream(File file, boolean append) 
创建文件输出流以写入由指定的 File对象表示的文件。  
FileOutputStream(FileDescriptor fdObj) 
创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。  
FileOutputStream(String name) 
创建文件输出流以指定的名称写入文件。  
FileOutputStream(String name, boolean append) 
创建文件输出流以指定的名称写入文件。  
 

//2. FileOutputStream往文件写入内容
FileOutputStream  fos = null;
        try {
            //1.创建IO对象
            fos = new FileOutputStream("d:/a.txt",true);
            //2.写
            String  str = "\nhello world\n";
            //需要把字符串转换为byte数组
            byte[] bytes = str.getBytes();
            fos.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
​

(2)字符节点流(.txt、.java能用记事本打开的文件)

1.FileReader文件符输入流

对文件进行操作:

FileInputStream(字节输入流),FileOutputStream(字节输出流),FileReader(字符输入流),FileWriter(字符输出流)
 

FileReader fr = null;
        try {
            //1.创建IO流对象
            fr = new FileReader("d:/hello.txt");
            //2.进行读写操作
            //int rs = fr.read();
            int rs = -1;
            while((rs = fr.read()) != -1){
                System.out.println((char)rs);
            }
        } catch (
                FileNotFoundException e) {
            e.printStackTrace();
        } catch (
                IOException e) {
            e.printStackTrace();
        }finally{
            //3.关闭
            try {
                if(fr != null){
                    fr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

对管道进行操作:

PipedInputStream(字节输入流),PipedOutStream(字节输出流),PipedReader(字符输入流),PipedWriter(字符输出流)
 

2.内存流

唯一一种从网络获取二进制数据的(意义不大、一般不存在内存里)

当输出流的目的和输入流的源是内存时,这样两端都是内存

1. ByteArrayInputStream把内存某个字节数组中数据读取出来
ByteArrayOutputStream把数据写到内存的字节数组中
 

 ByteArrayOutputStream boas = null;
        try {
            boas = new ByteArrayOutputStream();
            String str = "hello world";
            //写操作
            boas.write(str.getBytes());
            //刷新
            boas.flush();
​
            //得到字节数组
            byte[] b = boas.toByteArray();
​
            String s = new String(b);
            System.out.println(s);
​
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(boas!=null){
                try {
                    boas.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

3.缓冲流:(处理流)

不能直接接只能接到别的输入输出流

自带缓冲区、byte字节流、char[]字符流

1.BufferedReader

Buffered缓冲流:

 BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,是带缓冲区的处理流,缓冲区的作用的主要目的是:避免每次和硬盘打交道,提高数据访问的效率。

转化流:

InputStreamReader/OutputStreamWriter,把字节转化成字符。

数据流:

DataInputStream,DataOutputStream。
 
因为平时若是我们输出一个8个字节的long类型或4个字节的float类型,那怎么办呢?
 
可以一个字节一个字节输出,也可以把转换成字符串输出,但是这样转换费时间,若是直接输出该多好啊,因此这个数据流就解决了我们输出数据类型的困难。
 
数据流可以直接输出float类型或long类型,提高了数据读写的效率。

打印流:


printStream,printWriter,一般是打印到控制台,可以进行控制打印的地方。
 

对象流:


ObjectInputStream,ObjectOutputStream,把封装的对象直接输出,而不是一个个在转换成字符串再输出。
 

序列化流:


SequenceInputStream。
 
对象序列化:把对象直接转换成二进制,写入介质中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值