IO:如何使用FileReader来读取文件

来源:互联网 发布:mac两个窗口 编辑:程序博客网 时间:2024/05/22 11:50
IO:如何使用FileReader来读取文件:
package net.nyist.io;import java.io.FileReader;import java.io.IOException;public class FileReaderTest {public static void main(String[] args){try(//创建字符输出流,使用java7新特性,自动关闭资源FileReader fr =  new FileReader("src/net/nyist/io/FileReaderTest.java");){//创建一个长度为32的字符数组,数组不够大,需要多次读取,但是不会出现中文注释乱码问题char[] cbuf = new char[32];//用于保存实际读取的字符数int hasRead = 0;//使用循环读取数据while((hasRead = fr.read(cbuf)) > 0){//将字符数组转化为字符串输出System.out.println(new String(cbuf,0,hasRead));}}catch(IOException ex){ex.printStackTrace();}}}

 

原创粉丝点击