首页 > 其他 > 详细

用InputStream读出来转换成String类型

时间:2019-07-04 18:04:20      阅读:153      评论:0      收藏:0      [点我收藏+]
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();
                }
            }
          }
}

 

用InputStream读出来转换成String类型

原文:https://www.cnblogs.com/NuoChong/p/11133880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!