字节缓冲流的作用是,在应用程序写入的时候,可以一次性写入,而不需要为写入的每个字节都调用底层系统;
BufferedInputStream:创建bufferedinputstream将创建一个内部缓冲区数组,当从流中读取或者跳过字节时,内部缓冲区
将根据需要从所包含的输入流中国重新填充,一次多个字节;
BufferedoutputStream:输出缓冲流
new BufferedoutputStream(OutputStream out)
new BufferedInputStream(InputStream out)
字节缓冲流仅仅提供缓冲区,真正的读写数据还是需要依靠基本的字节流对象进行操作;
字节缓冲流使用方法---包含输入输出流;
public class bufferoutputstream {
public static void main(String[] args) throws IOException {
//创建输出流
FileOutputStream fos = new FileOutputStream("java.txt");
//创建字节缓冲输出流对象
BufferedOutputStream bos = new BufferedOutputStream(fos );
//写数据 ,方法与FileOutputStream一致,也是三种方法
bos.write("hello".getBytes());
bos.close();
//缓冲输出流,与输入流类似
FileInputStream fis = new FileInputStream("java.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//一次读取一个字节
/*int by;
while((by = bis.read())!=-1){
System.out.println((char) by);
}bis.close();*/
// y一次读取一个数组
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) !=-1){
System.out.print(new String(bys,0,len) );
}
bis.close();
}
}
复制视频;将视频文件从一个目录下复制到另一个目录下
第四中方法是最快的;
package file;
import java.io.*;
public class VedioCopy {
public static void main(String[] args) throws IOException {
//记录开始时间
long starttime = System.currentTimeMillis();
method1();
//记录结束时间
long endtime = System.currentTimeMillis();
long totaltime = endtime-starttime;
System.out.println(totaltime);
}
//复制视频
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("a.avi");
FileOutputStream fos = new FileOutputStream(".\\collectors\\b.avi");
//一次读写一个字节
int by;
while ((by =fis.read()) != -1){
fos.write(by);
}
fis.close();
fos.close();
}
//基本字节流一次读取一个字节数组
public static void method2 () throws IOException {
FileInputStream fis = new FileInputStream("ab.avi");
FileOutputStream fos = new FileOutputStream(".\\collections\\java.avi");
byte[] by = new byte[1024];
int len;
while ((len = fis.read(by))!= -1){
fos.write(by,0,len);
}fis.close();
fos.close();
}
//利用字节缓冲流一次读取一个字节
//利用字节缓冲流一次读取一个数组
public static void method4() throws IOException {
BufferedInputStream bif = new BufferedInputStream(new FileInputStream("filename"));
BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream("filenam"));
byte[] bys = new byte[1024];
int len ;
while((len = bif.read(bys)) != -1){
bof.write(bys,0,len);
}bif.close();bof.close();
}
}