/
FileUtils.java
131 lines (114 loc) 路 3.78 KB
/
FileUtils.java
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
package com.alibaba.cola.mock.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @author shawnzhan.zxy
* @date 2018/11/03
*/
public class FileUtils {
public static final String PACKAGE_SPLITER = ".";
public static final String PACKAGE_SPLITER_REGEP = "\\.";
public static File createFileIfNotExists(String srcResource){
File file = new File(srcResource);
File dir = file.getParentFile();
if(!dir.exists()){
dir.mkdirs();
}
try {
if(!file.exists()){
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public static File reCreateFile(String filePath) throws IOException {
File file = new File(filePath);
if(file.exists()){
file.delete();
}
file.createNewFile();
return file;
}
public static String convertPackage2Path(String packagePath){
packagePath = packagePath.replaceAll("\\.", "/");
return packagePath + "/";
}
public static String appendSlashToURILast(String srcResource){
srcResource.replaceAll("\\\\", "/");
if(!srcResource.endsWith("/")){
srcResource = srcResource + "/";
}
return srcResource;
}
public static void createDirectory(String srcResource){
String dirPath = srcResource;
if(!dirPath.endsWith("/")){
dirPath = dirPath.substring(0, dirPath.lastIndexOf("/"));
}
File file = new File(dirPath);
if(!file.exists()){
file.mkdirs();
}
}
public static String getDefaultDirectory4MockFile(){
String dir = FileUtils.class.getResource("/").getPath().replaceAll("target/test-classes", "src/test/resources/mockfile");
return appendSlashToURILast(dir);
}
public static String getDefaultDirectory4TestFile(){
String dir = FileUtils.class.getResource("/").getPath().replaceAll("target/test-classes", "src/test/java");
return appendSlashToURILast(dir);
}
/**
* 閲嶅缓鏂囦欢
* 鍒犻櫎+鏂板缓
* @param file
* @return
*/
public static File resoreFile(File file){
String filePath = file.getAbsolutePath();
if(file != null && file.exists()){
if(!file.delete()){
throw new RuntimeException("clean is error,delete file error!");
}
}
return createFileIfNotExists(filePath);
}
public static boolean isFileExists(String filePath){
File f = new File(filePath);
if(f.exists()){
return true;
}
return false;
}
public static InputStream getTestClassTemplate(){
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream("testclass-template.ftl");
}catch (Exception e) {
}
if(is == null){
is = FileUtils.class.getResourceAsStream("/META-INF/testclass-template.ftl");
}
return is;
}
/**
* 褰曞埗鏂囦欢鍚嶇缉鍐欙紝鍘熸湰com.alibaba.Test_execute->c.a.Test_execute
* @param originClassName
* @return
*/
public static String getAbbrOfClassName(String originClassName){
String[] classNameFragment = originClassName.split(PACKAGE_SPLITER_REGEP);
StringBuilder shortName = new StringBuilder();
for(int i=0; i<classNameFragment.length; i++){
if(i< classNameFragment.length-1){
shortName.append(new String(new char[]{classNameFragment[i].charAt(0)})).append(PACKAGE_SPLITER);
}else{
shortName.append(classNameFragment[i]);
}
}
return shortName.toString();
}
}