当前位置: 代码迷 >> 综合 >> FileReader-读取方式二
  详细解决方案

FileReader-读取方式二

热度:63   发布时间:2024-02-26 09:28:03.0
package cn.itcast.p3.io.filereader;import java.io.FileReader;
import java.io.IOException;//需求:读取一个文本文件。将读取到的字符打印到控制台.public class FileReaderDemo2 {
    /*** @param args* @throws IOException */public static void main(String[] args) throws IOException {
    FileReader fr = new FileReader("demo.txt");/** 使用read(char[])读取文本文件数据。* * 先创建字符数组。*/char[] buf = new char[1024];int len = 0;while((len=fr.read(buf))!=-1){
    System.out.println(new String(buf,0,len));}/*int num = fr.read(buf);//将读取到的字符存储到数组中。System.out.println(num+":"+new String(buf,0,num));int num1 = fr.read(buf);//将读取到的字符存储到数组中。System.out.println(num1+":"+new String(buf,0,num1));int num2 = fr.read(buf);//将读取到的字符存储到数组中。System.out.println(num2+":"+new String(buf));*/fr.close();}}