首页 > 编程语言 >java中IO流-缓冲流(字符型)复制操作

java中IO流-缓冲流(字符型)复制操作

时间:2022-09-21 10:16:18浏览次数:76  
标签:java br 缓冲 printStackTrace IOException IO catch new null

import java.io.*;

public class BufferedTest {
  public static void main(String[] args) {
        
        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader br = null;
        BufferedWriter bw = null;
        
        long start = System.currentTimeMillis();
        try {
            // 1.实例化File类的对象,指明要操作的文件
            File srcFile = new File("hello.txt");
            File destFile = new File("hello1.txt");
            
            // 2.1造字节流
            fr = new FileReader(srcFile);
            fw = new FileWriter(destFile);
            // 2.2造缓冲流
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            
            // 3.复制的细节:读取写入
            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) != -1) {
                bw.write(cbuf, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {// 4.关闭资源
            // 要求:先关闭外层的流,再关闭内层的流
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 可以省略(关闭外层流的同时,内层流也会自动的进行关闭)
//            if (fw != null) {
//                try {
//                    fos.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (fr != null) {
//                try {
//                    fis.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
        }
        long end = System.currentTimeMillis();
        System.out.println("复制花费的时间为:" + (end - start));
    }
}

 

标签:java,br,缓冲,printStackTrace,IOException,IO,catch,new,null
From: https://www.cnblogs.com/lxh-daniel/p/16714597.html

相关文章

  • 使用 JavaScript 的 Glassmorphic 计算器
    使用JavaScript的Glassmorphic计算器大家好!欢迎来到编码扭矩.在本博客中,我将向您解释如何使用HTML、CSS和JavaScript制作Glassmorphic计算器。这将是一个分......
  • JAVA线段树模板
    publicclassLineTree{int[]tree,nums;intn;publicLineTree(int[]nums){this.nums=nums;n=nums.length;tree=newi......
  • 探索Java8:(五)Supplier和Consumer接口的使用
    Supplier是函数式编程的另一个接口,与Function、Predicate接口类似,区别在于Supplier不接收任何参数,只返回结果。Supplier的基本使用@FunctionalInterfacepublicinterfac......
  • dotnet 为大型应用接入 ApplicationStartupManager 启动流程框架
    对于大型的应用软件,特别是客户端应用软件,应用启动过程中,需要执行大量的逻辑,包括各个模块的初始化和注册等等逻辑。大型应用软件的启动过程都是非常复杂的,而客户端应用软件......
  • java中IO流-缓冲流(字节型)复制操作
    importjava.io.*;publicclassBufferedTest{publicstaticvoidmain(String[]args){FileInputStreamfis=null;FileOutputStr......
  • java学习笔记day01
    笔记基础语法一、注释单行注释://123123多行注释:/*多行注释*/文档注释:/***@Description111*@Author111*/二、基本数据类型1、数据存储的单位​ 位、......
  • WebApplication.CreateBuilder(args); 当前上下文不存在WebApplication
       WebApplication.CreateBuilder(args);中的 WebApplication属于命名空间Microsoft.AspNetCore.Builder 但是引用之后,发现找不到对应的命名空间。原因是这个......
  • SpringIOC的理解
    IOC(InversionOfControl):控制反转控制:即对资源(如一个Java类)的获取方式获取方式可以分为两种主动获取在Spring之前我们想要获取一个类都是自己创建,即new出这个类......
  • java中IO流字节的读入与复制操作
    importjava.io.*;importorg.junit.Test;/**FileInputStream和FileOutputStream的使用*/publicclassFileInputOutputStreamTest{//使用字节流File......
  • 【Java UI】HarmonyOS添加日历事件
    ​参考资料CalendarDataHelperEventsRemindersapi讲解添加权限在config.json添加权限代码如下"reqPermissions":[{"name":"ohos.permission.RE......