java ReadUtils 工具类

package com.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.*;

@Slf4j
public class ReadUtils {


    /**
     * 生成文件夹
      * @param path  可传可不传
     * @return
     */
    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());
    }

    /**
     * 创建文件夹及文件 path
     * @param path 路径              非须传
     * @param name 文件名 eg : log   必须传
     */
    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());
        }
    }

    /**
     * 清除所有数据
     * @param path 可传可不传
     */
    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());
    }

    /**
     * 写入数据
     * @param path 可传可不传  传必须全路径
     * @param name 文件名 eg : log   必须传
     * @param str  写入的数据  必须
     */
    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);
    }

    /**
     *
     * @param path
     * @return
     */
    public static String url(String path){
        if(StringUtils.isEmpty(path)){
            path = DefContants.LOG_PATH;
        }
        return path;
    }

    public static void main(String[] args) {
        // 创建文件夹及文件 生成.ok 文件
        String nameOk = DefContants.getNameOk("XXXX");
        ReadUtils.mkdirFile(null,nameOk);
        // 写入.ok 文件
        ReadUtils.writeDate(null,nameOk,"1111");

        // 创建文件夹及文件 生成.txt 文件
        String name = DefContants.getName("BBBB");
        ReadUtils.mkdirFile(null,name);
        // 写入.txt 文件
        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";

    /**
     * 获取当前时间
     * @return
     */
    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;
    }

    /**
     * YMD
     * @return
     */
    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;
    }

    /**
     * replace
     * @return
     */
    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;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ks
 * @Date: 2021/08/11/9:41
 * @Description:
 */
@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";

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值