I/O实操之扩展

 

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

  

打印流分为PrintStream 和PrintWriter
        
     PrintStream :针对字节 调用println 内容中有换行符等   自动调用flush方法(自动发送缓冲区的内容到文件中)  
      PrintWriter:针对字符   只有在调用println 才会 自动调用flush方法(自动发送缓冲区的内容到文件中)
           
        
       

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//System.out.println()  打印到屏幕(标准输出设备)  建立在 PrintStream基础上
      File file=new File("d:/aa/1.txt");
       
      try {
           
          PrintWriter printWriter=new PrintWriter(file);
           
           
          printWriter.println("hello");
           
          printWriter.println("world");
           
          printWriter.close();
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
String filename=FilenameUtils.getName("d:/aa/1.txt"); //返回文件名
         
        System.out.println(filename);
         
        String extname=FilenameUtils.getExtension("d:/aa/1.txt"); //返回文件的扩展名(后缀名)
         
        System.out.println(extname);
         
        System.out.println(FilenameUtils.isExtension("d:/aa/0.jpg", "jpg"));//判断文件后缀名
         
         
        long begin=System.currentTimeMillis();
         
        try {
            FileUtils.copyFile(new File("d:/aa/0.jpg"), new File("d:/aa/1.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        long end=System.currentTimeMillis();
         
        System.out.println("用工具包复制文件用时:"+(end-begin)+"毫秒");
点击并拖拽以移动

  

properties

Properties是一个属性类,也许它可能在一些初级项目中很少运用,但是Properties文件,在许多项目中会经常运用,它使得我们的配置属性的独立化,方便项目后期的配置属性的更改。

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
//属性类  是一个集合  实现的是map接口
        Properties properties=new Properties();
         
        //存数据
        properties.put("type", "mysql");
         
        properties.setProperty("dbname", "taobao");
         
        properties.setProperty("uid", "mayun");
         
        //获取属性信息
        //System.out.println(properties.getProperty("uid"));
         
        //将集合中的信息保存到文件
        File file=new File("db.properties");
         
        FileWriter fileWriter;
        try {
             
            fileWriter = new FileWriter(file);
             
            //将集合中的数据保存到流中
            properties.store(fileWriter, "info");
             
            fileWriter.close();
             
             
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
点击并拖拽以移动

  

        

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        Properties properties=new Properties();
         
         
        try {
             
            //默认的路径是当前项目根目录
            FileReader fileReader=new FileReader("db.properties");
             
            properties.load(fileReader);
             
            System.out.println(properties.getProperty("uid"));
             
             
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
 

  

posted @   呆萌老师  阅读(22)  评论(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)
点击右上角即可分享
微信分享提示