JAVA基础--db19_javaIO流缓冲区

本文详细介绍了如何使用Java进行文件复制,包括字符流和字节流的方式,并深入探讨了缓冲区的应用及其对性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制:其实就是先读后写

public classHelloWorld {

   public static voidmain(String[] args) throws IOException{

      FileReader fr = newFileReader("C:\\QcOSD.txt");

      FileWriter fw = newFileWriter("D:\\demo.txt");

//    char[] buf = new char[1024];

      int len = 0;

      while ((len=fr.read())!=-1){

         fw.write(len);

      }

      fw.close();

      fr.close();

   }

}

上面是一个一个读的,下面按数组读:

public classHelloWorld {

   public static voidmain(String[] args){

      FileReader fr = null;

      FileWriter fw = null;

      try {

         fr = newFileReader("C:\\QcOSD.txt");

         //写的文件有就覆盖,没有就创建

         fw = newFileWriter("D:\\demo.txt");

         //创建一个缓存容器,用来缓存读取到的字符

         char[] buf = new char[1024];

         //定义一个变量记录读取到的字符数,其实就是数组里的字符个数

         int len = 0;

         while((len=fr.read(buf))!=-1){

            fw.write(buf, 0, len);

         }

      } catch(Exception e) {

         throw newRuntimeException("错了");

      } finally{

         if(fw!=null)

            try {

                fw.close();

            } catch(IOException e) {

                e.printStackTrace();

            }

         if(fr!=null)

         try {

            fr.close();

         } catch(IOException e) {

            e.printStackTrace();

         }

      }

   }

}

复制原理图:


字符流缓冲区:

 

public classHelloWorld {

  

   private static final String LINE_SEPARATOR = System.lineSeparator();

 

   public static voidmain(String[] args) throws IOException{

      FileWriter fw = newFileWriter("d:\\demo.txt");

      //关联缓冲区

      BufferedWriter bufw = newBufferedWriter(fw);

      bufw.write("asdf"+LINE_SEPARATOR+"ghjkl");

      bufw.write("xixixixi");

      //缓冲区自带的换行方法newline()

      bufw.newLine();

      bufw.write("hahahahh");

      //缓冲区关的时候,fw也自动关掉了,缓冲区只是提高效率,底层关的是fw

      bufw.close();

   }

}

上面是写,下面是读:

public classHelloWorld {

  

   private static final String LINE_SEPARATOR = System.lineSeparator();

 

   public static voidmain(String[] args) throws IOException{

      FileReader fr = newFileReader("c:\\QcOSD.txt");

      //他有一个特殊的方法,一次读取一行

      BufferedReader bufw = newBufferedReader(fr);

      String line = null;

      while((line=bufw.readLine())!=null){

         System.out.println(line);

      }

   }

}

装饰设计模式:类似装修,就是将一个事物的外观改变,实质没变

Buffer就是用装饰设计模式设计出来的

下3图为装饰事例:


----

package db_01;

//这个类是为了增强person而出现的

public class NewPerson{

   //增强他,所以肯定有Person

   private Person p;

   NewPerson(Person p){

      this.p = p;

   }

   public void chifan(){

      System.out.println("开胃酒");

      p.chifan();

      System.out.println("甜点");

   }

}

----

package db_01;

 

public class Person {

   Person(){}

   void chifan(){

      System.out.println("吃饭");

   }

}

 

----

运行结果:


----

 

装饰和缓冲区的区别:


这里的text和media仅仅是例子,不真实存在

继承:


装饰:


Buffered的一个装饰子类:LineNumberReader


字节流:

字节流的数组用的是byte[]   字节流用的是char[]


FileInputStream有一个特殊的方法available(),这个方法可以返回数据的长度

此方法只适应于小文件,不适应于大文件

字节流操作媒体文件练习:

方式一:

public classHelloWorld {

   public static voidmain(String[] args) throws IOException{

      String str = "F:\\photo\\0.png";

      String str1 = "F:\\photo\\00.png";

      FileInputStream fis = newFileInputStream(str);

      FileOutputStream fos = newFileOutputStream(str1);

      byte[] buf = new byte[1024];

      int len = 0;

      while ((len=fis.read(buf))!=-1) {

         fos.write(buf, 0, len);

      }

      fos.close();

      fis.close();

   }

}

 

方式二:

public classHelloWorld {

   public static voidmain(String[] args) throws IOException{

      String str = "F:\\photo\\0.png";

      String str1 = "F:\\photo\\00.png";

      FileInputStream fis = newFileInputStream(str);

      BufferedInputStream bufis = newBufferedInputStream(fis);

      FileOutputStream fos = newFileOutputStream(str1);

      BufferedOutputStream bufos =newBufferedOutputStream(fos);

      byte[] buf = new byte[1024];

      int len = 0;

      while ((len=bufis.read(buf))!=-1) {

         bufos.write(buf, 0, len);

         //缓冲区的刷新是复写过的,所以有用

         bufos.flush();

      }

      bufos.close();

      bufis.close();

   }

}

这些文本,音乐,图片都可以说是文件,所以都是filexxx类

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值