创建文件文件名重复问题

文件创建,输入文件需要存放的路径名称,目录不存在自动创建目录,若目录存在直接创建文件,若该文件名称的文件已经存在,这在该文件名称后追加数字序号创建文件。

 1 package com.wb.fileutils;
 2 
 3 
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6 
 7 import java.io.File;
 8 import java.io.IOException;
 9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11 
12 public class FileUtil {
13     //日志打印
14     private final static Logger logger = LoggerFactory.getLogger(FileUtil.class);
15 
16 
17     /**
18      * 传入文件全路径
19      * 创建文件,文件重复追加后缀,
20      *
21      * @param path
22      */
23     public static String createFile(String path) {
24         int count = 1;
25         String suffix = "";
26         String toPrefix = "";
27         File file = new File(path);
28         String fileName = file.getName();
29         if (file.exists()) {//文件存在追加后缀
30             //获取文件后缀
31             int index = fileName.lastIndexOf(".");
32             if (index != -1)
33                 suffix = fileName.substring(index);
34             if (suffix.length() != 0) {
35                 toPrefix = fileName.substring(0, fileName.indexOf(suffix.replace(".", "")) - 1);
36             } else {
37                 toPrefix = fileName;
38             }
39             logger.debug("文件后缀:" + ((suffix.length() > 0) ? suffix : "文件不存在后缀"));
40             logger.debug("文件前缀toPrefix:" + toPrefix);
41             String regex = "[(][0-9]+[)]$";
42             Pattern pat = Pattern.compile(regex);
43             Matcher mat = pat.matcher(toPrefix);
44             if (mat.find()) {
45                 String end = mat.group();
46                 count = Integer.valueOf(end.substring(1, end.length() - 1)) + 1;
47                 toPrefix = toPrefix.substring(0, mat.start()) + "(" + count + ")";
48             } else {
49                 toPrefix = toPrefix + "(" + count + ")";
50             }
51             return createFile(toPrefix + suffix);
52         } else {//文件不存在创建文件
53             try {
54                 file.createNewFile();
55             } catch (IOException e) {
56                 // e.printStackTrace();
57                 logger.error(e.getMessage());
58             }
59             return path;
60         }
61     }
62 
63     /**
64      * 传入文件全路径,创建对应目录
65      *
66      * @param path
67      */
68     public static void createDirectory(String path) {
69         File file = new File(path);
70         String filePath = file.getPath();
71         logger.debug(filePath);
72         String directoryPath = "/";
73         if (filePath.lastIndexOf("\") != -1) {
74             directoryPath = filePath.substring(0, filePath.lastIndexOf("\"));
75         }
76         File directory = new File(directoryPath);
77         logger.debug(directoryPath);
78         //判断目录是否存在
79         if (!directory.exists()) {
80             //创建文件
81             directory.mkdirs();
82         }
83     }
84 
85 
86     public static void main(String[] args) {
87         // for (int i = 0; i < 15; i++) {
88         //String file = createFile("test.txt");
89 
90 //        System.out.println(file);
91         //createDirectory("test.txt");
92         //}
93     }
94 }
原文地址:https://www.cnblogs.com/lingdu9527/p/11611247.html