Java IO之一读取文件
2016-11-21 16:15 甘雨路 阅读(148) 评论(0) 编辑 收藏 举报package com.lf.iopreoject; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; public class TestCode { public static void main(String[] args) { TestCode.testFileInputStream(); } public static void testFileInputStream() { //读取一个文件 File file = new File("song.txt"); BufferedReader br = null; try { String str; FileReader fr = new FileReader(file); br = new BufferedReader(fr); // 读取的结果不为空就在控制台打印 while ((str = br.readLine()) !=null) { System.out.println(str); } } catch (Exception e) { // TODO: handle exception }finally{ if (br!=null) { try { br.close(); } catch (Exception e) { // TODO: handle exception } } } } }