1.计算机如何存储中文的?
当前平台eclipse默认编码集 :GBK 一个中文对应两个字节
第一个字节:一定是负数
第二个字节:一般是负数,可能也会是正数,不会影响结果.
2.一次读取一个字节数组的方式要比一次读取一个字节方式高效.
一次读取一个字节数组,相当于构造一个缓冲区,有没有比一次读取一个字节数组还要高效的流? 字节缓冲流
一.字节缓冲输出流
构造方式:
public BufferedOutputStream(OutputStream out):采用的默认的缓冲区大小(足够大了) ,来构造一个字节缓冲输出流对象
public BufferedOutputStream(OutputStream out,int size):指定size缓冲区大小构造缓冲输出流对象
IllegalArgumentException - 如果 size <= 0
写数据的方式:
一次写一个字节
write(int by)
一次写一个字节数组的一部分
write(byte[] b, int off, int len)
方法:
void flush() ;刷新缓冲区的流
//构造一个字节缓冲输出流对象
//public BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt")) ;
//写数据
bos.write("hello".getBytes());
//释放资源
bos.close();
面试题:
字节缓冲输出流它的构造方法为什么不能直接传递路径/文件?
缓冲输入流/缓冲输出流,它只是在底层内部提供一个缓冲区的数组,底层实现文件的复制/读取/写入这些操作都依赖于基本流对象来操作(InputStream/OutputStream/FileInputStream/FileOutputstream)
二.字节缓冲输入流
构造方式:
public BufferedInputStream(InputStream in):默认缓冲区大小构造缓冲输入流对象
public BufferedInputStream(InputStream in,int size):指定缓冲区大小构造缓冲输入流对象
读数据方式:
public int read()
public int read(byte[] b,int off,int len)
//构造一个字节缓冲输入流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
//读数据
//一次读取一个字节
/*int by = 0 ;
while((by=bis.read())!=-1) {
System.out.print((char)by);
}*/
//一次读取一个字节数组
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len=bis.read(bys))!=-1) {
System.out.println(new String(bys, 0, len));
}
//释放资源
bis.close();
注:在使用输入流的时候, 两种方式读取(一次读取一个字节/一次读取一个字节数组),只能用一种方式,否则,会出现错误!
3. 基本的字节流:文件字节输入流/文件字节输出流
高效的字节流(缓冲流):字节缓冲输入流/字节缓冲输出流
操作一个视频文件,来测试速度问题
* 基本的字节流一次读取一个字节 :共耗时:85772毫秒
* 基本的字节流一次读取一个字节数组 :共耗时:216毫秒
* 高效的字节流一次读取一个字节 :共耗时:682毫秒
* 高效的字节流一次读取一个字节数组:共耗时:49毫秒
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis() ;
method1("e:\\abc.mp4","copy1.mp4") ;
method2("e:\\abc.mp4","copy2.mp4") ;
method3("e:\\abc.mp4","copy3.mp4") ;
method4("e:\\abc.mp4","copy4.mp4") ;
long end = System.currentTimeMillis() ;
System.out.println("共耗时:"+(end-start)+"毫秒");
}
//高效的流一次读取一个字节数组
private static void method4(String src, String dest) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)) ;
//一次读取一个字节数组
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len=bis.read(bys))!=-1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
//高效的字节流一次读取一个字节
private static void method3(String src, String dest) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)) ;
//一次读个字节
int by = 0 ;
while((by=bis.read())!=-1) {
bos.write(by);
}
//释放资源
bis.close();
bos.close();
}
//基本的字节流一次读取一个字节数组
private static void method2(String src, String dest) throws Exception {
//封装文件
FileInputStream fis = new FileInputStream(src) ;
FileOutputStream fos = new FileOutputStream(dest) ;
//读写操作
byte[] bys = new byte[1024] ;//相当于一个缓冲区
int len = 0 ;
while((len=fis.read(bys))!=-1) {
fos.write(bys, 0, len);
}
//释放资源
fis.close();
fos.close();
}
//基本的字节流一次读取一个字节
private static void method1(String src, String dest) throws Exception {
//封装源文件和目标文件
FileInputStream fis = new FileInputStream(src) ;
FileOutputStream fos = new FileOutputStream(dest) ;
//读写操作
int by = 0 ;
while((by=fis.read())!=-1) {
//写
fos.write(by);
}
//释放资源
fis.close();
fos.close();
}
从上述例子中,可以看出加入缓冲流操作效率远高于基本流!开发中常用缓冲流进行操作!
4.存储文件
IO流:永久存储(耗时)
数据库:永久存储(效率高)