您的位置:首页 > 其它

IO(五、FileReader第二种读数据)char[] buf=new char[1024];

2015-10-12 16:48 309 查看
import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

//通过字符数组读取

public class FileReader2 {

public static void main(String[] args) {

FileReader fr=null;

try {

fr = new FileReader("G:\\demo.txt");

//定义一个字符数组,用于存储读到字符

char[] buf=new char[1024];

/*//fr.read(buf)把读出的字符存到数组,返回的是读到的字符个数

int num=fr.read(buf);

System.out.println(num+" "+new String(buf));*/

int num=0;

while((num=fr.read(buf))!=-1){

System.out.println(new String(buf,0,num));//从0开始取,取num个

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally{

try {

fr.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: