File.io读取文件(四)

来源:互联网 发布:mac物理地址不匹配 编辑:程序博客网 时间:2024/06/06 11:17
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;


public class FileRead4 {
/**
* 文件-(I流+转换流+缓冲流)-->程序--->字节数组
* 转换流,解码,指定字符集(字节到字符的桥梁)
* 缓冲流提高性能
* @throws IOException 
*/
public static void main(String[] args) throws IOException {

//1,建立文件与程序的联系
File f=new File("D:/a.txt");
//2,选择流
BufferedInputStream/*InputStream*/ is=new BufferedInputStream(
new FileInputStream(f));
//=========
ByteArrayOutputStream bis=new ByteArrayOutputStream();
//此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。
//=========
//3,操作数据
byte[] flush=new byte[31/*1024*/];
int len=0;
while(-1!=(len=is.read(flush))){
bis.write(flush, 0, len);
}
bis.flush();//
byte[] dest=bis.toByteArray();
System.err.println("在这里是所有的数据,长度"+dest.length+Arrays.toString(dest));
is.close();
}
}
0 0
原创粉丝点击