Java—字节缓冲流
缓冲流都是为了增强读写的效率
BufferedOutputStream 字节缓冲输出流
例:
例:
public static void main(String[] args) throws IOException {
FileOutputStream fis = new FileOutputStream("/Users/apple/Desktop/FileTest/e.txt");
BufferedOutputStream bos = new BufferedOutputStream(fis);
bos.write("123456787654321234567654321`".getBytes());
bos.close();
}
BufferedInputStream 字节缓冲输入流
例:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/apple/Desktop/FileTest/e.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b))!=-1){
System.out.println(new String(b,0,len));
}
bis.close();
}
缓冲流复制文件
例:
public static void main(String[] args) throws IOException {
long l = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/Users/apple/Desktop/FileTest/e.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/Users/apple/Desktop/FileTest/f.txt"));
int len = 0;
byte[] b = new byte[1024];
while ((len = bis.read(b))!=-1){
//System.out.println(new String(b,0,len));
bos.write(b,0,len);
}
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("需要:"+(e-l)+"毫秒");
}