文件处理类


title: 文件处理类
date: 2018-08-28 20:56:00
tags:
- 工具
- 文件

categories: [Java 工具]


1.生成文件名,避免文件重复

public static String getRandomFileName() {
    // 生成随机文件名:当前年月日时分秒+五位随机数(为了在实际项目中防止文件同名而进行的处理)
    int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000; // 获取随机数
    String nowTimeStr = sDateFormat.format(new Date()); // 当前时间
    return nowTimeStr + rannum;
}

2.删除文件或目录

public static void deleteFile(String storePath) {
    File file = new File( storePath);
    if (file.exists()) {
        if (file.isDirectory()) {
            File files[] = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                files[i].delete();
            }
        }
        file.delete();
    }
}

3.读取文件内容到string中

    public static String readFileToStr(String path){
    StringBuffer stringBuffer=new StringBuffer();
    try {
        BufferedReader in = new BufferedReader(new FileReader(path));
    String line=    in.readLine();
    while (line!=null){
        stringBuffer.append(line);
        line=in.readLine();
    }
    in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return stringBuffer.toString();
}

4.读取配置文件内容到map,可通过map读取对应配置

     public static HashMap<String, String> readFiletoMap(String path) {
        HashMap<String, String> propeMap = new HashMap<String, String>();
        String[] tem;
        String line="";
        try {
            BufferedReader in = new BufferedReader(new FileReader(path));
            line = in.readLine();
            while (line != null) {
               tem=line.split("=");
               if (tem.length>1)
                propeMap.put(tem[0],tem[1]);
                line = in.readLine();
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return propeMap;
    }

Coding Blog     Github Blog

                                                                                                                                                                           
                                                                                                                                                                           
                                                                                               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值