IOl数据流中的字节流
IO流分类
- 根据按照数据流向 站在内存角度
输入流 读入数据
输出流 写出数据
- 按照数据类型分为字节流和字符流
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件
- 字节流又分为字节输入流 InPutStream和字节输出流OutPutStream
其中 输入流与输出流是一一对应的关系FileInPutStream —FileOutPutStrStream
ObjectInputStream----ObjectOutPutStream
- 通过文件输出流来关联文件写入数据
一次读取一个字节
FileOutputStream out = new FileOutputStream("a.txt");
out.write(97);
out.close();
一次读取一个数组
FileOutputStream out = new FileOutputStream("a.txt");
byte[] bytes1 = new byte[1024];
out.write(bytes1);
out.close();//其中的close用于释放资源,必须要加上
- 字节流可用于文件或者文件夹的复制
FileInputStream in = new FileInputStream("MyTest.java");
FileOutputStream out = new FileOutputStream("E:\\MyTest.java");
int len=0;
while ((len=in.read())!=-1){
out.write(len);
out.flush();//刷新
}
in.close();
out.close();
- 高效的字节流读取 BufferedInputStream
BufferedInputStream bfr = new BufferedInputStream(new FileInputStream("demo.mp3"));
BufferedOutputStream bfw = new BufferedOutputStream(new FileOutputStream("demo2.mp3"))
int len=0;
while ((len=bfr.read())!=-1){
bfw.write(len);
out.flush();//刷新
}
bfr.close();
bfw.close();