文本文件读取

读取文本文件

1. 关联文件,使用Reader 和 FileReader

2. 创建缓冲 char数组,用于接收读取到的文本信息

3. 将文本读入到 缓冲数组(buff)中

4. 输出读取到的文本信息

5. 关闭文件读入流

package com.machuang.io.charIO;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class textRead {

    public static void main(String[] args) {
        // 与文件建立联系
        Reader reader = null;
        
    
        try {
            reader = new FileReader("F:/win10/test/a.txt");
            
            // 创建接收读取内容的 char数组
            char[] cbuf = new char[1024];
            int len = 0;
            
            // 读取
            while(-1 != (len = reader.read(cbuf))) {
                System.out.println(new String(cbuf, 0, len));
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
                
        }
        

    }    // match main function

}
原文地址:https://www.cnblogs.com/cappuccinom/p/8809738.html