黑马程序员技术交流社区

标题: 测试题7 [打印本页]

作者: 郁金香大公爵    时间: 2015-7-30 09:19
标题: 测试题7
/**
* 第七题: 定义一个文件输入流,调用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();
    }
   }
  }
}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2