java中IO流(BufferedInputStream和BufferOutputStream拷贝)
* A:缓冲思想
* 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
* 这是加入了数组这样的缓冲区效果,java本身在设计的时候,
* 也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流
* B.BufferedInputStream
* BufferedInputStream内置了一个缓冲区(数组)
* 从BufferedInputStream中读取一个字节时
* BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个
* 程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
* 直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
* C.BufferedOutputStream
* BufferedOutputStream也内置了一个缓冲区(数组)
* 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
* 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("致青春.mp3");//创建输入流对象,关联致青春.mp3
FileOutputStream fos = new FileOutputStream("copy.mp3");//创建输出流对象,关联copy.mp3
BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象,对输入流进行包装让其变得更加强大
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
IO流(flush和close方法的区别)
* flush()方法
* 用来刷新缓冲区的,刷新后可以再次写出
* close()方法
* 用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出
IO流(字节流读写中文)
* 字节流读取中文的问题
* 字节流在读中文的时候有可能会读到半个中文,造成乱码
* 字节流写出中文的问题
* 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
* 写出回车换行 write("\r\n".getBytes());
public class Demo6_Chinese {
/**
* @param args
* * 字节流读取中文的问题
* 字节流在读中文的时候有可能会读到半个中文,造成乱码
* 字节流写出中文的问题
* 字节流直接操作的字节,所以写出中文必须将字符串转换成字节数组
* 写出回车换行 write("\r\n".getBytes());
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("zzz.txt");
fos.write("我读书少,你不要骗我".getBytes());
fos.write("\r\n".getBytes());
fos.close();
}
###20.14_IO流(流的标准处理异常代码1.6版本及其以前)
* try finally嵌套
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("aaa.txt");
fos = new FileOutputStream("bbb.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
} finally {
try {
if(fis != null)
fis.close();
}finally {
if(fos != null)
fos.close();
}
}
###20.15_IO流(流的标准处理异常代码1.7版本)
* try close
try(
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
* 原理
* 在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用,流对象的close方法将流关掉
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo7_TryFinally {
/**
* @param args
* @throws IOException
1.7版本就可以用这种写法
*/
public static void main(String[] args) throws IOException {
//demo1();
try(
FileInputStream fis = new FileInputStream("xxx.txt");
FileOutputStream fos = new FileOutputStream("yyy.txt");
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
}
/**IO流的异常标准处理代码**/
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("yyy.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}finally {
try{
if(fis != null)
fis.close();
}finally {//try fianlly的嵌套目的是能关一个尽量关一个
if(fos != null)
fos.close();
}
}
}
}