现在的位置: 首页 > 综合 > 正文

java中的FileReader

2017年11月09日 ⁄ 综合 ⁄ 共 503字 ⁄ 字号 评论关闭

第一种

public class FileReaderDemo {

	public static void main(String[] args) throws IOException {
		
		FileReader fr = new FileReader("test.txt");
		
		int ch = 0;
		while((ch=fr.read())!=-1){
			System.out.println((char)ch);
		}
		
		fr.close();
	}

}

第二种

public class FileReaderDemo2 {

	
	public static void main(String[] args) throws IOException {
		
		FileReader fr = new FileReader("test.txt");
		
		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));
		*/
		fr.close();
	}
	
}

抱歉!评论已关闭.