FileInputStream读取文件的方法

FileInputStream i=null;
        try {
            //读取文件
            i=new FileInputStream("out/work1");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            //需要关闭,不关闭浪费资源
            if (i!= null) {
                i.close();
            }
        }
        //创建一个数组长度为4的数组
        byte[] b=new byte[4];
        //i.read(b)计算读取了多少个元素,如果最后没有元素了,则返回-1
        int c=0;
        while ((c=i.read(b)) != -1){
            //这个String方法  中间是从数组下标为0的开始读取,b是读取4个,c是得出用数组读取了几个元素
            System.out.println(new String(b,0,c));
        }

你可能感兴趣的:(java,java)