I/O实操

 

1
2
3
4
5
6
7
8
9
10
11
12
/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*vx:it_daimeng
*/

  

1、复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
File file=new File("d:/aa/0.jpg");
         
        File toFile=new File("d:/aa/00.jpg");
         
        long begin=System.currentTimeMillis();
         
         
        try {
            //创建一个字节输入流 用来读取原文件的内容
            FileInputStream fileInputStream=new FileInputStream(file);
             
            //创建一个字节输出流 用来写入复制后的文件
            FileOutputStream fileOutputStream=new FileOutputStream(toFile);
             
            int a;
            //从输入流读
            while((a=fileInputStream.read())!=-1)
            {
                //通过输出流写出去
                fileOutputStream.write(a);
            }
             
            fileInputStream.close();
             
            fileOutputStream.close();
             
             
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        long end=System.currentTimeMillis();
         
        System.out.println("复制总用时:"+(end-begin)+"毫秒");
        

  

当我们使用文件流之后,直接通过流直接将数据写入另一个文件当中,这样虽然可以实现复制文件的目的但是这样的操作显得尤为粗暴不优雅。

2、缓冲流复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//用缓冲流复制文件
File file=new File("d:/aa/0.jpg");
 
File toFile=new File("d:/aa/000.jpg");
 
long begin=System.currentTimeMillis();
 
//创建缓冲字节输入流 (一定要先有字节输入流)
FileInputStream fileInputStream;
try {
     
    fileInputStream = new FileInputStream(file);
    BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
     
    //创建缓冲字节输出流
    FileOutputStream fileOutputStream=new FileOutputStream(toFile);
    BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
     
    //从输入流中读取数据 从输出流写出去
    int a;
    while((a=bufferedInputStream.read())!=-1)
    {
         bufferedOutputStream.write(a);
    }
     
    bufferedInputStream.close();
     
    bufferedOutputStream.close();
     
     
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 
long end=System.currentTimeMillis();
 
System.out.println("复制文件总用时:"+(end-begin)+"毫秒");

  

3、不断从一个文件读取数据写入到另一个文件


       

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
File file=new File("d:/aa/person.java");
       
      File toFile=new File("d:/aa/person2.java");
       
      long begin=System.currentTimeMillis();
      try {
          //创建缓冲字符输入流(先创建一个字符输入流)
          FileReader fileReader =new FileReader(file);
          BufferedReader bufferedReader=new BufferedReader(fileReader);
           
          //创建缓冲字符输出流(先创建一个字符输出流)
          FileWriter fileWriter=new FileWriter(toFile);
          BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
           
          //循环从一个地方读出 写到另一个文件
          String str;
          while((str=bufferedReader.readLine())!=null)
               bufferedWriter.write(str);
           
          bufferedReader.close();
           
          bufferedWriter.close();
           
           
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

  


        
        long end=System.currentTimeMillis();
        System.out.println("使用缓冲字符流复制文件总用时:"+(end-begin)+"毫秒");
        
 

posted @   呆萌老师  阅读(19)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示