zoukankan      html  css  js  c++  java
  • 字节输入流-InputStream demo5

    package inputstream.cn;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    /*
     * 第五种读取方式:文件读到末尾了,会返回-1
     * 得到len的值,还给b数组赋值
     *     int temp;
            int len = 0;
            while ((temp = is.read())!=-1) {
                b[len] = (byte)temp;
                len++;
            }
     * 
     */
    public class InputStreamDemo5 {
        public static void main(String[] args) throws Exception {
            //使用file 找到一个文件
            File f = new File("d:"+File.separator+"test.txt");
            //通过子类实例化父类
            InputStream is =new FileInputStream(f);
            //开辟一个1024的字节数组,把所有的类容读到此数组中
            byte[] b = new byte[1024];
            int temp;
            int len = 0;
            while ((temp = is.read())!=-1) {
                b[len] = (byte)temp;
                len++;
            }
            //关闭输入流
            is.close();
            //打印读的数据,将byte类型转换为string类型输出
            System.out.println(new String(b,0,len));
            
        }
    }
  • 相关阅读:
    【Dos-BatchPrograming】04
    【Dos-BatchPrograming】03
    【Dos-BatchPrograming】02
    【Dos-BatchPrograming】01
    【perl】01
    【Linux】Re04
    【Linux】Re03
    【Linux】Re02
    【Linux】Re01
    【C++】01
  • 原文地址:https://www.cnblogs.com/yuanyuan2017/p/6950347.html
Copyright © 2011-2022 走看看