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

Java基础篇IO流总结[补充]

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

Java基础篇IO流总结[补充]

IO流总结目录
  • 1. 转换流
    • 1.1 转换流概述
    • 1.2 转换流典型使用
    • 1.3 标准的输入输出过程
  • 2. 其他流的使用
    • 2.1 标准的输入输出流
    • 2.2 打印流
    • 2.3 数据流
  • 3. 对象流
    • 3.1 对象流的概述
    • 3.2 对象流的使用

1. 转换流 1.1 转换流概述

【说明】
①转换流提供了在字节流和字符流之间的转换
②转换流使用的两个类:
    InputStreamReader:将字节输入流转换为字符输入流,解码:字节、字节数组 —> 字符数组、字符串
    OutputStreamReader:将字符输出流转换为字节输出流,编码:字符数组、字符串 —> 字节、字节数组编码决定了解码
③作用:
    (1)很多时候我们使用转换流来处理文件乱码问题。实现编码和解码的功能。
    (2)字节流中的数据都是字符时,转成字符流操作更高效。
④图示方式

1.2 转换流典型使用

【使用说明】
转换流在使用转换流和缓冲流的时候,在创建转换流和缓冲流流对象的时候,new使用的参数用的都是FileInputStream、FileOutputStream
实现的步骤就是标准的四步(下方阐述):

	@Test
    public void testInputStreamOutputStream() {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
            //创建操作的对象
            File srcfile = new File("hello.txt");
            File destfile = new File("hello4.txt");

            //在使用转换流的时候,是在节点流的基础之上的,所以首相要先建立节点流
            FileInputStream fis = new FileInputStream(srcfile);
            FileOutputStream fos = new FileOutputStream(destfile);
            isr = new InputStreamReader(fis);
            osw = new OutputStreamWriter(fos);

            //对数据进行操作
            char[] cbuf = new char[20];
            int len;
            while ((len = isr.read(cbuf)) != -1){
                osw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null){
                //由于上方流资源关闭的时候,下方对应的流资源也会关闭,所以在操作的时候只需要关注上方的流资源就可以了
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

在指明编码的方式的情况下,在进行解码时也需要使用相同的方式

			isr = new InputStreamReader(fis,"UTF-8");
//            osw = new OutputStreamWriter(fos,"GBK");//GBK会出现乱码
            osw = new OutputStreamWriter(fos,"UTF-8");
1.3 标准的输入输出过程

标准输入过程

① 创建File类的对象,指明读取的数据来源。(此文件一定存在)
② 创建相应的输入流,将File类的对象作为参数,传入流的构造器中
③具体的读入过程
     创建相应的byte[] 或 char[]
     使用while循环,完成读取
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally来进行处理。

标准输出过程

输出过程
① 创建File类的对象,指明写出的数据位置。(此文件不一定存在)
② 创建相应的输出流,将File类的对象作为参数,传入流的构造器中
③具体的写出过程
     创建相应的byte[] 或 char[]
     使用while循环,完成写出
④ 关闭流资源
说明:程序中出现的异常需要使用try-catch-finally来进行处理。

2. 其他流的使用 2.1 标准的输入输出流

【了解内容】
【说明】

①System.in和System.out分别代表了系统标准的输入和输出设备
默认输入设备是:键盘,输出设备是:显示器
② System.in的类型是InputStream
③System.out的类型是PrintStream
④重定向:通过System类的setIn,setOut方法对默认设备进行改变

典型使用:
题目描述
     从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序

代码实现

public static void main(String[] args) throws IOException {

        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        while (true) {
            System.out.println("请输入字符串:");
            String data = br.readLine();//读取一行数据
            if (data.equalsIgnoreCase("e") || data.equalsIgnoreCase("exit")) {
                System.out.println("程序结束");
                break;
            }
            String s = data.toUpperCase();
            System.out.println(s);
        }
        br.close();
    }
2.2 打印流

【了解内容】
【说明】

①提供了一系列重载的print()和println()方法,用于多种数据类型的输出
②System.out返回的是PrintStream的实例

使用样例
     在使用时为了方便查看步骤这里使用的是throws的方式处理异常,在真正的开发过程中使用的是try-catch-finally的方式处理

	@Test
    public void test2() throws FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("E:\io\test.txt");//指明打印位置
        PrintStream ps = new PrintStream(fos);
        if (ps != null) {
            System.setOut(ps);//把标准输出流改成文件
        }
        for (int i = 1; i <= 255; i++) {
            System.out.print((char) i);
            if (i % 50 == 0) {
                System.out.println();
            }
        }
        ps.close();
    }
2.3 数据流

【了解内容】
【说明】
     ①为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。
     ②数据流有两个类:(用于读取和写出基本数据类型、String类的数据)DataInputStream 和 DataOutputStream, 分别“套接”在 InputStream 和 OutputStream 子类的流上

使用举例:

//实现写的过程
    @Test
    public void test3() {

        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("text.txt"));
            dos.writeUTF("黎明");
            dos.flush();
            dos.writeInt(22);
            dos.flush();
            dos.writeBoolean(true);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //实现读的过程
    //读取的顺序要和写入的顺序一致
    @Test
    public void test4() {
        DataInputStream dis = null;

        try {
            //造数据流
            dis = new DataInputStream(new FileInputStream("text.txt"));
            //2.操作数据
            String name = dis.readUTF();
            int age = dis.readInt();
            boolean isMale = dis.readBoolean();
            System.out.println("neme = " + name + ",age = " + age + ",isMale = " + isMale);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
3. 对象流 3.1 对象流的概述

【说明】

①使用到的类:ObjectInputStream和OjbectOutputSteam
②用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
③涉及到了序列化
     序列化:用ObjectOutputStream类保存基本类型数据或对象的机制
     反序列化:用ObjectInputStream类读取基本类型数据或对象的机制(在使用的过程中,需要先进行序列化,保存到硬盘中才可以进行反序列化,进行读取)
④对象的序列化:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。

3.2 对象流的使用

【序列化过程】
实现样例:
在这里直接创建了一个String类型的对象,没有另外创建别的对象

	@Test
    public void testObjectOutputStream(){
        ObjectOutputStream oos = null;

        try {
            //创建流和对象
            oos = new ObjectOutputStream(new FileOutputStream("Object.dat"));

            //实现数据的操作(创建对象并且添加进去)
            oos.writeObject(new String("我学到对象流了"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //实现关闭操作
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

【反序列化过程】
将磁盘层面的对象还原到内存层面的对象,可以输出到控制台,查看保存的内容
实现样例:

	@Test
    public void testObjectInputStream()  {
        ObjectInputStream ois = null;

        try {
            //创建流和对象
            ois = new ObjectInputStream(new FileInputStream("Object.dat"));
            //分开写的步骤如下:
//        File file = new File("Object.dat");
//        FileInputStream fis = new FileInputStream(file);
//        ObjectInputStream ois = new ObjectInputStream(fis);

            //操作数据
            Object object = ois.readObject();
            System.out.println(object);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //关闭流资源
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/931069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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