From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/FileUtil.java | 181 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 181 insertions(+), 0 deletions(-) diff --git a/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/FileUtil.java b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/FileUtil.java new file mode 100644 index 0000000..d44ef4b --- /dev/null +++ b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/util/FileUtil.java @@ -0,0 +1,181 @@ +package com.xxl.job.core.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * file tool + * + * @author xuxueli 2017-12-29 17:56:48 + */ +public class FileUtil { + private static Logger logger = LoggerFactory.getLogger(FileUtil.class); + + + /** + * delete recursively + * + * @param root + * @return + */ + public static boolean deleteRecursively(File root) { + if (root != null && root.exists()) { + if (root.isDirectory()) { + File[] children = root.listFiles(); + if (children != null) { + for (File child : children) { + deleteRecursively(child); + } + } + } + return root.delete(); + } + return false; + } + + + public static void deleteFile(String fileName) { + // file + File file = new File(fileName); + if (file.exists()) { + file.delete(); + } + } + + + public static void writeFileContent(File file, byte[] data) { + + // file + if (!file.exists()) { + file.getParentFile().mkdirs(); + } + + // append file content + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + fos.write(data); + fos.flush(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + } + + } + + public static byte[] readFileContent(File file) { + Long filelength = file.length(); + byte[] filecontent = new byte[filelength.intValue()]; + + FileInputStream in = null; + try { + in = new FileInputStream(file); + in.read(filecontent); + in.close(); + + return filecontent; + } catch (Exception e) { + logger.error(e.getMessage(), e); + return null; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + } + } + + + /*public static void appendFileLine(String fileName, String content) { + + // file + File file = new File(fileName); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + return; + } + } + + // content + if (content == null) { + content = ""; + } + content += "\r\n"; + + // append file content + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file, true); + fos.write(content.getBytes("utf-8")); + fos.flush(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + } + + } + + public static List<String> loadFileLines(String fileName){ + + List<String> result = new ArrayList<>(); + + // valid log file + File file = new File(fileName); + if (!file.exists()) { + return result; + } + + // read file + StringBuffer logContentBuffer = new StringBuffer(); + int toLineNum = 0; + LineNumberReader reader = null; + try { + //reader = new LineNumberReader(new FileReader(logFile)); + reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "utf-8")); + String line = null; + while ((line = reader.readLine())!=null) { + if (line!=null && line.trim().length()>0) { + result.add(line); + } + } + } catch (IOException e) { + logger.error(e.getMessage(), e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } + } + + return result; + }*/ + +} -- Gitblit v1.9.3