zoukankan      html  css  js  c++  java
  • FileInputStream中read()方法在参数不同时的返回值说明

    read()空参数,作用是“从此输入流中读取一个数据字节。”,返回值为读取到的字节并强转为int形式

    public class P01Stream {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File f = new File("D:\lol2.txt");//“ABC”
            FileInputStream fis = new FileInputStream(f);
            int len = 0;
            
            while((len = fis.read()) != -1)
                System.out.println(len);        
        }
    }
    //输出结果:
    //65
    //66
    //67

    read(byte[] b)时,作用是“从此输入流中将最多b.length 个字节的数据读入一个 byte 数组中。”返回值是读取到的字节个数。

    public class P01Stream {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File f = new File("D:\lol2.txt");//"ABC"
            FileInputStream fis = new FileInputStream(f);
            int len = 0;
            byte[] bytes = new byte[(int) f.length()];
            
            while((len = fis.read(bytes)) != -1)
                System.out.println(len);        
        }
    }
    //输出结果
    //3

    该byte数组作为缓冲数组,存储了读取到的字节

    public class P01Stream {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            File f = new File("D:\lol2.txt");//"ABC"
            FileInputStream fis = new FileInputStream(f);
            int len = 0;
            byte[] bytes = new byte[(int) f.length()];
            
            while((len = fis.read(bytes)) != -1) {
                for (byte b : bytes) {
                    System.out.println(b);
                }
                
            }
                        
        }
    }
    //输出结果
    //65
    //66
    //67
  • 相关阅读:
    54-Spiral Matrix
    查找、排序算法
    Java中Timer的使用
    2017/5/8总结 js数组及json(对象数组)操作
    asp.net 中文件上传的两种方式
    2017/4/24combox 使用
    2017/4/24 静态页,评论,购物车总结
    2017/4/23星期日 总结
    2017.4.16关于在线图书商城注册界面总结
    c#中的委托和事件
  • 原文地址:https://www.cnblogs.com/try4396/p/12157349.html
Copyright © 2011-2022 走看看