0
点赞
收藏
分享

微信扫一扫

Java中的FileReader 文件字符输入流

young_d807 2022-03-19 阅读 55
java
/*文件字符输入流  只能读取普通文本
*  读取文本内容的时候,比较方便和便捷*/
public class FileReaderTest {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            /*读入的文件的目录,这个文件只能是普通的文本文件*/
            fr = new FileReader("E:\\IDEA\\Java\\StudentTwo\\src\\IOTest\\JavaTest.txt");
            /*一次读入4个字符*/
            char[] chars = new char[4];
            int readCount =0;

            while(  (readCount = fr.read(chars)) !=-1){
                System.out.println( new String(chars,0,readCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

另外!!!!!

char[] chars = new char[4];

fr.read(chars);

for(char c: chars){
 System.out.println(c);

}

这就说明了读的时候是按字符一次读入一个字符,按字符方式读取 

举报

相关推荐

0 条评论