InputStream的三个read()方法

老是搞混,记下来。忘记了方便翻出来看看

View Code
 1 import java.io.File;
2 import java.io.FileInputStream;
3 public class ReadFileDemo
4 {
5 public static void main(String[] args) throws Exception
6 {
7 File file=new File("c:"+File.separator+"data.txt");
8 FileInputStream input=new FileInputStream(file);
9
10 //InputStream的三个read方法的应用
11 byte[] b=new byte[(int)file.length()]; //计算文件的字节长度
12 int len=input.read(b,0,b.length);//读取指定的字节数量到数组
13 System.out.println(new String(b));
14 System.out.println(b.length==len);
15
16 // byte[] b=new byte[1024];
17 // int len=input.read(b);//一次性读取,返回总共读取的字节数
18 // System.out.println(new String(b,0,len));
19 // System.out.println(len);
20
21 // byte[] b=new byte[(int)file.length()];
22 // for(int i=0;i<b.length;i++)
23 // {
24 // b[i]=(byte)input.read();//返回的是读取的数据,逐个字节读取内容
25 // }
26 // System.out.println(new String(b));
27 }
28 }



原文地址:https://www.cnblogs.com/xiongyu/p/2244152.html