FileUtil.java
1.69 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
package com.topdraw.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
/**
* @author :
* @description:
* @function :
* @date :Created in 2022/5/12 11:43
* @version: :
* @modified By:
* @since : modified in 2022/5/12 11:43
*/
public class FileUtil {
private static void createFile(String filePath){
File testFile = new File(filePath);
File fileParent = testFile.getParentFile();//返回的是File类型,可以调用exsit()等方法
//String fileParentPath = testFile.getParent();//返回的是String类型
if (!fileParent.exists()) {
fileParent.mkdirs();// 能创建多级目录
}
if (!testFile.exists()) {
try {
testFile.createNewFile();//有路径才能创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeStringToFile2(String filePath, String content, String error) {
try {
String property = System.getProperty("user.dir");
filePath = property + filePath;
createFile(filePath);
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(LocalDateTime.now()+"\n");
bw.write("【content】==>> \n"+content+"\n");
bw.write("【error】==>> \n"+error+"\n");
bw.write("----------------------------------------------------------------\n");
bw.close();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}