FileReader读取文件的两种方式

一个字符一个字符的读,读一个打印一个

package com.zkk.reader;

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

public class Read_One_Demo {
    private static FileReader fr=null;
    public static void main(String[] args){
        try{
        fr=new FileReader("text.demo");
            int num=0;
            while((num=fr.read())!=-1){
                System.out.println((char)num);
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
    }

}

先读到一个数组里,再打印这个数组

package com.zkk.reader;

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

public class Read_Two_Demo {
    private static FileReader fr=null;
    public static void main(String[] args) {
        try{
            fr=new FileReader("text.demo");
                int num=0;
                char[]buf=new char[1024];
                while((num=fr.read(buf))!=-1){
                    System.out.println(new String(buf,0,num));
                }
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                
            }
    }

}

原文地址:https://www.cnblogs.com/zk753159/p/5021907.html