IO流5 --- FileReader讀入數據的基本操做 --- 技術搬運工(尚硅谷)

  • FileReader 字符輸入流
@Test
public void test1(){
    File file = new File("hello.txt");
    FileReader fr = null;
    try {
        fr = new FileReader(file);
        //返回讀取的內容
        int data;
        while ((data = fr.read()) != -1){
            System.out.print((char)data);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (fr != null){
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
相關文章
相關標籤/搜索