诺虫的箱子

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
public class demo {
    public static void main(String[] args) {
        //先把图片读入到内存--然后写到 文件
         FileInputStream fis=null;     
            File q=new File("e:/123.txt");
            //因为File没有读写的能力,所以需要使用InputStream;
            try {
                fis=new FileInputStream(q);
                //定义一个字节数组,相当于缓存
                byte[] bytes=new byte[1024];
                int n=0;//得到实际读取到的字节数 读到最后返回-1
                //循环读取
                while((n=fis.read(bytes))!=-1)//把fis里的东西读到bytes数组里去
                {
                    //把字节转成String 从0到N变成String
                    String w=new String(bytes,0,n);
                    System.out.println(w);

                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                //一定要关闭文件流。并且关闭文件流必须放在finally里面
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
          }
}

 

posted on 2019-07-04 17:42  诺虫的箱子  阅读(689)  评论(0)    收藏  举报