InputStream is = new FileInputStream(“E:\\test.txt”);
//构建输出流对象(写文件)
OutputStream os = new FileOutputStream(“E:\\ctest.txt”);
//声明一个大小为输入流大小的数组
byte[] b = new byte[is.available()];
i
nt len = 0;//表示成功读取的字节数的个数
while(len<is.available()){
//read(byte[] b,int off, int len)表示从输入流当中最多将 len 个字节的数据读
取到一个
byte 数组当中
len+=is.read(b, len, is.available() - len);
os.write(b);
}
}catch{
e.printStackTrace();
}
}
运行上面的程序控制台的输出为:
则一次性读取完成
2、public static void inputStreamRead3(){
Try{
//构建输入流对象(读文件)
InputStream is = new FileInputStream(“E:\\test.txt”);
//构建输出流对象(写文件)
OutputStream os = new FileOutputStream(“E:\\ctest.txt”);
//声明一个大小为输入流大小的数组
byte[] b = new byte[1024];
int len = 0;//表示成功读取的字节数的个数
while((len=is.read(b,len,1024))!=-1){
System.out.println(“len=”+len);