package FileT.Day3;
/**
* @author CCQ
* 字节缓冲流
* 将要读或者要写的数据放入到缓冲区,然后再一次性读或写。能够提高性能。
* BufferedOutputStream();里面共可以有两个参数,一个是原本的字节流对象,一个是int类型的缓冲区长度,不写就是默认大小8192
* BufferedInputStream();里面共可以有两个参数,一个是原本的字节流对象,一个是int类型的缓冲区长度,不写就是默认大小8192
* 用法跟字节流一样
*/
import java.io.*;
import java.util.Arrays;
public class BufferStreamTest {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("fox.txt"));
bos.write(("I think you will like it!").getBytes());
BufferedInputStream bis =new BufferedInputStream(new FileInputStream("fox.txt"));
bos.close();
//一次读一个字节
int by;
while((by =bis.read())!=-1){
System.out.print((char) by);
}
//一次读一组字节
byte[] b =new byte[1024];
int len;
while((len =bis.read(b,0,b.length))!=-1){
System.out.println(new String(b,0,len));
}
bis.close();
}
}
//复制视频小demo
class demo{
public static void main(String[] args) throws IOException {
BufferedInputStream bis =new BufferedInputStream(new FileInputStream("C:\\Users\\CCQ\\Documents\\Tencent Files\\953881082\\FileRecv\\递归.mp4"));
BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("C:\\Users\\CCQ\\Documents\\Tencent Files\\953881082\\FileRecv\\递归1.mp4"));
int len;
byte[] b =new byte[1024];
while((len =bis.read(b,0,b.length))!=-1){
bos.write(b,0,len);
}
bis.close();
bos.close();
}
}
//编码解码小demo
class demo1{
public static void main(String[] args) throws UnsupportedEncodingException {
String s ="朱雀";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
String ss =new String(bytes);
System.out.println(ss);
String s1 ="终极迈凯伦";
byte[] bytes1 = s1.getBytes("GBK");
System.out.println(Arrays.toString(bytes1));
String ss2 =new String(bytes1,"GBK");
System.out.println(ss2);
}
}
字节缓冲流
最新推荐文章于 2024-07-13 08:10:19 发布