Java ReadUtils 工具类

188 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

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");
    }


}