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");
}
}
package com.util;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Component
public class DateUtils {
public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_YMD = "yyyyMMdd";
public static String getTime(){
try{
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_FULL);
String format = df.format(new Date());
return format;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String getYMD(){
try{
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_YMD);
String format = df.format(new Date());
return format;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String replaceTime (){
try{
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_FULL);
String format = df.format(new Date());
return format.replace(" ","").replace(":","").replace("-","");
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
package com.util;
import lombok.Data;
@Data
public class DefContants {
public static String getName(String str){
return DateUtils.getYMD() + "_" + str + ".txt";
}
public static String getNameOk(String str){
return DateUtils.getYMD() + "_" + str + ".txt.ok";
}
public static final String LOG_PATH ="C:\\tmp\\work\\log";
}