文本读取方式一 练习笔记

来源:互联网 发布:用友u8初始化数据库 编辑:程序博客网 时间:2024/05/19 09:03
package test.io;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


/**
 * @author //文本读取方式一 
 *
 */
public class FileReaderDemo { 
public static void main(String[] args) throws IOException {
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
FileReader fr=new FileReader("demo.txt");
//调用读取流对象的read方法。
//read();一次读一个字符,而且会自动往下读。
/*int ch1=fr.read();
System.out.println("ch1="+(char)ch1);
int ch2=fr.read();
System.out.println("ch2="+(char)ch2);
int ch3=fr.read();
System.out.println("ch3="+(char)ch3);
int ch4=fr.read();
System.out.println("ch4="+(char)ch4);*/
//文本读取方式一 
int ch=0;
while ((ch=fr.read())!=-1) {
System.out.println((char)ch);
}
fr.close();


}


}
0 0
原创粉丝点击