A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 郁金香大公爵 中级黑马   /  2015-7-30 09:19  /  166 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/**
* 第七题: 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)。
*
* @author
*
*
*/
public class Test7 {
public static void main(String[] args) {
  FileInputStream fr = null;
  try {
   fr = new FileInputStream("d:\\exercise.txt");
   byte[] buf = new byte[5];
   int len = 0;
   while ((len = fr.read(buf)) != -1) {
    for (int i = 0; i < len; i++) {
     System.out.print((char) buf[i]);
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (fr != null) {
    try {
     fr.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马