黑马程序员技术交流社区

标题: 黑马入学考试题 5 [打印本页]

作者: nagi    时间: 2014-9-18 12:38
标题: 黑马入学考试题 5
定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)。

大家都是什么做的,,,有木有考虑中文乱码的问题啊!!!:#
作者: 默默丶    时间: 2014-9-18 17:19
  1. import java.io.*;
  2. public class Demo
  3. {
  4.         public static void main(String[] args)throws IOException
  5.         {
  6.                 FileInputStream fis = new FileInputStream("exercise.txt");
  7.                 PrintStream fos =System.out;
  8.                 byte[] buf =new byte[5];
  9.                 int len = 0;
  10.                
  11.                 while((len=fis.read(buf))!=-1)
  12.                 {
  13.                         fos.write(buf,0,len);
  14.                         fos.flush();
  15.                 }
  16.                 fos.close();
  17.                 fis.close();
  18.         }
  19. }
复制代码





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