本文已参与「新人创作礼」活动,一起开启掘金创作之路。
package com.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.*;
@Slf4j
public class ReadUtils {
public static void mkdirFolder(String path) {
path = url(path);
File file = new File(path);
if (!file.exists() && !file.isDirectory()) {
try {
log.debug("file success !!!");
file.mkdirs();
} catch (Exception e) {
e.printStackTrace();
log.debug("file error:"+e.getMessage());
}
} else {
}
log.debug("-------file create---------"+file.getAbsolutePath());
}
public static void mkdirFile(String path,String name){
path = url(path);
File file = new File(path);
File file1 = new File(file,name);
if(!file.exists()){
file.mkdirs();
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
log.debug("-------file create---------"+file1.getAbsolutePath());
}
}
public static void claerFile(String path) {
path = url(path);
File file = new File(path);
try {
FileOutputStream fos = new FileOutputStream(new File(path));
fos.write(new String("").getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
log.debug("-------file create---------"+file.getAbsolutePath());
}
public static void writeDate(String path,String name,String str) {
String key =""+DateUtils.getTime()+" : "+str + "\n";
try {
if(StringUtils.isEmpty(path)){
path = DefContants.LOG_PATH + "\\" + name;
}
FileOutputStream fos = new FileOutputStream(new File(path),true);
fos.write(key.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
log.debug("-------file write---------"+path);
}
public static String url(String path){
if(StringUtils.isEmpty(path)){
path = DefContants.LOG_PATH;
}
return path;
}
public static void main(String[] args) {
String nameOk = DefContants.getNameOk("XXXX");
ReadUtils.mkdirFile(null,nameOk);
ReadUtils.writeDate(null,nameOk,"1111");
String name = DefContants.getName("BBBB");
ReadUtils.mkdirFile(null,name);
ReadUtils.writeDate(null,name,"1111");
}
}