parent
8567a4c93f
commit
a07001644b
@ -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;
|
||||||
|
// }*/
|
||||||
|
//
|
||||||
|
//}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
//package com.xxl.job.core.util;
|
||||||
|
//
|
||||||
|
//import org.slf4j.Logger;
|
||||||
|
//import org.slf4j.LoggerFactory;
|
||||||
|
//
|
||||||
|
//import java.io.*;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * @author xuxueli 2020-04-12 0:14:00
|
||||||
|
// */
|
||||||
|
//public class JdkSerializeTool {
|
||||||
|
// private static Logger logger = LoggerFactory.getLogger(JdkSerializeTool.class);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// // ------------------------ serialize and unserialize ------------------------
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 将对象-->byte[] (由于jedis中不支持直接存储object所以转换成byte[]存入)
|
||||||
|
// *
|
||||||
|
// * @param object
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static byte[] serialize(Object object) {
|
||||||
|
// ObjectOutputStream oos = null;
|
||||||
|
// ByteArrayOutputStream baos = null;
|
||||||
|
// try {
|
||||||
|
// // 序列化
|
||||||
|
// baos = new ByteArrayOutputStream();
|
||||||
|
// oos = new ObjectOutputStream(baos);
|
||||||
|
// oos.writeObject(object);
|
||||||
|
// byte[] bytes = baos.toByteArray();
|
||||||
|
// return bytes;
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// logger.error(e.getMessage(), e);
|
||||||
|
// } finally {
|
||||||
|
// try {
|
||||||
|
// oos.close();
|
||||||
|
// baos.close();
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// logger.error(e.getMessage(), e);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 将byte[] -->Object
|
||||||
|
// *
|
||||||
|
// * @param bytes
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static <T> Object deserialize(byte[] bytes, Class<T> clazz) {
|
||||||
|
// ObjectInputStream ois = null;
|
||||||
|
// ByteArrayInputStream bais = null;
|
||||||
|
// try {
|
||||||
|
// // 反序列化
|
||||||
|
// bais = new ByteArrayInputStream(bytes);
|
||||||
|
// ois = new ObjectInputStream(bais);
|
||||||
|
// return ois.readObject();
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// logger.error(e.getMessage(), e);
|
||||||
|
// } finally {
|
||||||
|
// try {
|
||||||
|
// ois.close();
|
||||||
|
// bais.close();
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// logger.error(e.getMessage(), e);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
//package com.xxl.job.core.util;
|
||||||
|
//
|
||||||
|
//import java.io.PrintWriter;
|
||||||
|
//import java.io.StringWriter;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * @author xuxueli 2018-10-20 20:07:26
|
||||||
|
// */
|
||||||
|
//public class ThrowableUtil {
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * parse error to string
|
||||||
|
// *
|
||||||
|
// * @param e
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static String toString(Throwable e) {
|
||||||
|
// StringWriter stringWriter = new StringWriter();
|
||||||
|
// e.printStackTrace(new PrintWriter(stringWriter));
|
||||||
|
// String errorMsg = stringWriter.toString();
|
||||||
|
// return errorMsg;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//}
|
||||||
@ -1,181 +0,0 @@
|
|||||||
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;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,75 +0,0 @@
|
|||||||
package com.xxl.job.core.util;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xuxueli 2020-04-12 0:14:00
|
|
||||||
*/
|
|
||||||
public class JdkSerializeTool {
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(JdkSerializeTool.class);
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------------ serialize and unserialize ------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将对象-->byte[] (由于jedis中不支持直接存储object所以转换成byte[]存入)
|
|
||||||
*
|
|
||||||
* @param object
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static byte[] serialize(Object object) {
|
|
||||||
ObjectOutputStream oos = null;
|
|
||||||
ByteArrayOutputStream baos = null;
|
|
||||||
try {
|
|
||||||
// 序列化
|
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
oos = new ObjectOutputStream(baos);
|
|
||||||
oos.writeObject(object);
|
|
||||||
byte[] bytes = baos.toByteArray();
|
|
||||||
return bytes;
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
oos.close();
|
|
||||||
baos.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将byte[] -->Object
|
|
||||||
*
|
|
||||||
* @param bytes
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static <T> Object deserialize(byte[] bytes, Class<T> clazz) {
|
|
||||||
ObjectInputStream ois = null;
|
|
||||||
ByteArrayInputStream bais = null;
|
|
||||||
try {
|
|
||||||
// 反序列化
|
|
||||||
bais = new ByteArrayInputStream(bytes);
|
|
||||||
ois = new ObjectInputStream(bais);
|
|
||||||
return ois.readObject();
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
ois.close();
|
|
||||||
bais.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
package com.xxl.job.core.util;
|
|
||||||
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xuxueli 2018-10-20 20:07:26
|
|
||||||
*/
|
|
||||||
public class ThrowableUtil {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* parse error to string
|
|
||||||
*
|
|
||||||
* @param e
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String toString(Throwable e) {
|
|
||||||
StringWriter stringWriter = new StringWriter();
|
|
||||||
e.printStackTrace(new PrintWriter(stringWriter));
|
|
||||||
String errorMsg = stringWriter.toString();
|
|
||||||
return errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in new issue