IO流——文件操作流之字符输入流FileReader

    xiaoxiao2026-08-18  8

    package com.io.ioDemo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; //字符流读文件 public class FileReaderDemo { public static void main(String[] args) { FileReader fr = null; //1.创建一个字符流对象 try { fr = new FileReader(new File("c:\\aa.txt")); //2.定义一个字符数组作为缓存区域 char[] bt = new char[1024]; //3.定义一个变量存储读取的字符的数量 int hasread = 0; //4.循环获取数据 //StringBuffer sb = new StringBuffer(); try { while((hasread = fr.read(bt))!=-1){ //5.把每一步获取的数据都放入StringBuilder中 System.out.println(new String(bt,0,hasread)); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ if(fr!=null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
    转载请注明原文地址: https://ju.6miu.com/read-1311297.html
    最新回复(0)