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

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();
                }
            }
            
        }
        
    }

}

FileReader读取文件的两种方式

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

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{
                
            }
    }

}

FileReader读取文件的两种方式

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2021-11-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-09-17
  • 2022-02-07
猜你喜欢
  • 2021-12-27
  • 2022-01-17
  • 2021-12-24
  • 2021-11-23
  • 2022-01-24
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案