package IODemo;import java.io.*;
import java.nio.charset.StandardCharsets;/*** @author Alina* @date 2021年10月18日 9:57 下午* 一、 1.IO流关于缓冲区,* 2.输出流缓冲区:BufferedOutputStream(OutputStream out)* 3.输入流缓冲区:BufferedInputStream(InputStream in)* 4.提高流对象传递效率***/
public class FileIOStream {public static void main(String[] args) {long startime = System.currentTimeMillis();try {method_copy3();} catch (Exception e) {e.printStackTrace();}long endtime = System.currentTimeMillis();System.out.println(endtime - startime);}public static void method_bufferedout() throws Exception {//先创建OutputStream()对象,缓冲区方法依赖于流对象FileOutputStream fos = new FileOutputStream("/Users/yuzhang/Desktop/test.txt");BufferedOutputStream bos = new BufferedOutputStream(fos);bos.write("你好".getBytes(StandardCharsets.UTF_8));bos.close();}public static void method_bufferedin() throws Exception {FileInputStream fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");BufferedInputStream bis = new BufferedInputStream(fis);
// int s = 0;
// while ((s = bis.read()) != -1){
// System.out.print((char)s);
// }
// bis.close();byte[] bytes = new byte[1024];int x = 0;while ((x = bis.read(bytes)) != -1) {System.out.print(new String(bytes, 0, x));}bis.close();}public static void method_copy1() {//先创建两个流向,先赋值为空,以避免作用域不同导致无法使用FileInputStream fis = null;FileOutputStream fop = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fop = new FileOutputStream("src/IOdemo/test1.txt");//创建字节int bytes = 0;while ((bytes = fis.read()) != -1) {fop.write(bytes);}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("复制失败");} finally {try {if (fop !=null) {fop.close();}} catch (Exception e) {e.printStackTrace();throw new RuntimeException("复制失败");} finally {try{if (fis != null) {fis.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("关闭数据源失败");}}}}public static void method_copy2(){//读写字节数组FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("/Users/yuzhang/Desktop/test.txt");fos = new FileOutputStream("src/IOdemo/test1.txt");byte[] bytes = new byte[1024];int len = 0;while ((len = fis.read(bytes))!= -1){fos.write( bytes,0,len);}}catch (Exception e){e.printStackTrace();throw new RuntimeException("复制失败");}finally {try{if(fos !=null ){fos.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("关闭资源失败");}finally {try {if (fis != null){fis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("关闭资源失败");}}}}public static void method_copy3(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{bis = new BufferedInputStream(new FileInputStream("/Users/yuzhang/Desktop/test.txt"));bos = new BufferedOutputStream(new FileOutputStream("src/IOdemo/test1.txt"));byte[] bytes = new byte[1024];int len = 0;while ((len = bis.read(bytes)) != -1){bos.write(bytes,0,len);}}catch (Exception e ){e.printStackTrace();throw new RuntimeException(" 复制失败");}finally {try{if (bos != null){bos.close();}}catch (Exception e ){e.printStackTrace();throw new RuntimeException("资源关闭异常");}finally {try{if (bis != null){bis.close();}}catch (Exception e){e.printStackTrace();throw new RuntimeException("资源关闭异常");}}}}}