JAVA基础复习-字符流FileRead

IO流主要分为四大类,虽然流的处理类有很多,但是都是从四个基本的抽象类中派生而来的

四种抽象类的IO流:

  1. InputStream
  2. OutputStream
  3. Read
  4. Writer
    本节使用字符流FileRead来写一个标准的示例,以后其它的流也可参考此步骤书写

读入操作:

package com.winson.iotest;

import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @description: 字符流FileRead使用示例
 * @date: 2020/7/5 16:50
 * @author: winson
 */
public class FileReadWriterTest {

    /**
     * 1、read()的理解:返回读入的一个字符,如果达到文件末尾,返回-1
     * 2、异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
     * 3、读入的文件一定要存在,否则就会报FileNotFoundException
     */
    @Test
    public void test1() {
        FileReader fileReader = null;
        try {
            //1、实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");
            //2、提供具体的流
            fileReader = new FileReader(file);
            //获取流的字符编码
            String encoding = fileReader.getEncoding();
            System.out.println(encoding);
            //3.读取流中的数据
//        方式一
//        int read = fileReader.read();
//        while (read != -1) {
            //将代表字符的数字转为字符
//            System.out.print((char) read);
//            read = fileReader.read();
//        }
//        方式二
            int data;
            while ((data = fileReader.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭操作
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * test1()方法的升级写法,更有效率
     */
    @Test
    public void test2() {
        FileReader fileReader = null;
        try {
            //1、实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");
            //2、提供具体的流
            fileReader = new FileReader(file);
            //获取流的字符编码
            String encoding = fileReader.getEncoding();
            System.out.println(encoding);
            //3.读取流中的数据
//        方式一
            //错误的写法:结果为:helloworld123ld
//            char[] chars = new char[5];
//            int len;
//            while ((len = fileReader.read(chars)) != -1) {
//                for (int i = 0; i < chars.length; i++) {
//                    System.out.print(chars[i]);
//                }
//            }
//            方式一
            //正确的写法
            char[] chars = new char[5];
            //len:记录每次读入到chars数组中的字符的个数
            int len;
            while ((len = fileReader.read(chars)) != -1) {
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
            }
////        方式二
//            int data;
//            while ((data = fileReader.read()) != -1) {
//                System.out.print((char) data);
//            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭操作
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

posted @ 2020-07-05 17:58  温森  阅读(500)  评论(0编辑  收藏  举报