forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
152 lines (132 loc) · 3.93 KB
/
Copy pathFileUtil.java
File metadata and controls
152 lines (132 loc) · 3.93 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.myframework.util;
import java.io.File;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.util.StringUtils;
public class FileUtil {
private static final Log log = LogFactory.getLog(FileUtil.class);
public static String FILE ="FILE_" ;
/**创建目录
* @param filePath
*/
public static void createDir(String filePath) {
if (!new File(filePath).mkdirs())
log.error("创建目录 fail");
else
log.warn("创建目录 success");
}
/**根据文件绝对路径来创建文件
* @param file
* @throws Exception
*/
public static void createFile(String file) throws Exception {
File f = new File(file);
if (f.exists()) {
log.debug("文件名:" + f.getAbsolutePath());
log.debug("文件大小:" + f.length());
} else {
f.getParentFile().mkdirs();
f.createNewFile();
}
}
/**根据全类名获得文件路径
* @param className 类名
* @return
*/
public static String getJavaFileName(String className) {
log.debug("className :" +className);
String javaFileName = StringUtils.getPackageAsPath(getPackage(className)) +getClassName(className)+ ".java";
return javaFileName;
}
/**根据全类名获得JAVA文件所在目录
* @param className
* @return
*/
public static String getPackage(String className) {
return className.substring(0, className.lastIndexOf("."));
}
/**根据全类名获得简短类名
* @param className
* @return
*/
public static String getClassName(String className) {
return className.substring( className.lastIndexOf(".")+1);
}
/**根据文件对象获取扩展 名
* @param f
* @return
*/
public static String getExtension(File f) {
return (f != null) ? getExtension(f.getName()) : "";
}
/**根据文件名获取扩展名(默认方法)
* @param filename
* @return
*/
public static String getExtension(String filename) {
return getExtension(filename, "");
}
/**根据文件名获取扩展名,无扩展名时返回默认扩展名
* @param filename
* @param defExt
* @return
*/
public static String getExtension(String filename, String defExt) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i >-1) && (i < (filename.length() - 1))) {
return filename.substring(i + 1);
}
}
return defExt;
}
/**去除给定文件名的扩展名部分
* @param filename
* @return
*/
public static String trimExtension(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i >-1) && (i < (filename.length()))) {
return filename.substring(0, i);
}
}
return filename;
}
/**利用时间戳重命名中文文件
* @param fileName
* @return
*/
public static String renameFile(String prefix,String fileName) {
StringBuffer newFileName = new StringBuffer(prefix!=null?prefix:FILE);
newFileName.append(DateUtil.format(new Date(), "yyyyMMddHHmmssms"))
.append(".").append(getExtension(fileName));
return newFileName.toString();
}
/**利用时间戳重命名中文文件
* @param fileName
* @return
*/
public static String renameFile( String fileName) {
return renameFile(null,fileName);
}
/**利用时间戳重命名中文文件
* @param file
* @return
*/
public static String renameFile(String prefix,File file) {
StringBuffer newFileName = new StringBuffer(prefix!=null?prefix:FILE);
newFileName.append(DateUtil.format(new Date(), "yyyyMMddHHmmssms"))
.append(".").append(getExtension(file));
return newFileName.toString();
}
public static void main(String[] args) throws Exception {
String className = "ims.tools.web.action.taskAction";
String fileName = FileUtil.getJavaFileName(className);
log.debug(DateUtil.getNowTime());
log.debug(renameFile("abc.txt.xls"));
// System.out.println(fileName);
// FileUtils.createFile("D:/"+ fileName);
}
}