2023-01-10 16:02:56 +08:00
|
|
|
|
package com.mesasoft.cn.sketch.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author yjy
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
* @date 2021/2/25 6:11 下午
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class FileUtils {
|
|
|
|
|
|
private static final Logger LOG = Logger.getLogger(FileUtils.class);
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static List<String> readTxtFileIntoStringArrList(String filePath) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
List<String> list = new ArrayList<>();
|
2023-01-29 16:36:18 +08:00
|
|
|
|
try {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
String encoding = "GBK";
|
|
|
|
|
|
File file = new File(filePath);
|
2023-01-29 16:36:18 +08:00
|
|
|
|
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
2023-01-10 16:02:56 +08:00
|
|
|
|
InputStreamReader read = new InputStreamReader(
|
|
|
|
|
|
new FileInputStream(file), encoding);
|
|
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(read);
|
|
|
|
|
|
String lineTxt = null;
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
while ((lineTxt = bufferedReader.readLine()) != null) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
if (!lineTxt.equals("")) {
|
|
|
|
|
|
list.add(lineTxt.trim());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
bufferedReader.close();
|
|
|
|
|
|
read.close();
|
2023-01-29 16:36:18 +08:00
|
|
|
|
} else {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
System.out.println("Can not find file: " + filePath);
|
|
|
|
|
|
}
|
2023-01-29 16:36:18 +08:00
|
|
|
|
} catch (Exception e) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
System.out.println("Error occurred in Function 'readTxtFileIntoStringArrList'");
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static List<String> getBatchLineReadIn(BufferedReader bufferedReader, int batchSize) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
List<String> list = new ArrayList<>();
|
|
|
|
|
|
String lineTxt;
|
2023-01-29 16:36:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
while ((lineTxt = bufferedReader.readLine()) != null && list.size() < batchSize) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
if (!lineTxt.equals("")) {
|
|
|
|
|
|
list.add(lineTxt.trim());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-29 16:36:18 +08:00
|
|
|
|
} catch (IOException e) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static void createFile(File filePath, String fileName) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
try {
|
|
|
|
|
|
File file = new File(filePath.toString() + "/" + fileName);
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
if (!filePath.exists()) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
filePath.mkdirs();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boolean isCreate = file.createNewFile();
|
2023-01-29 16:36:18 +08:00
|
|
|
|
if (isCreate) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
LOG.info("File " + fileName + " is created.");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static void createFile(File file) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
try {
|
|
|
|
|
|
boolean isCreate = file.createNewFile();
|
2023-01-29 16:36:18 +08:00
|
|
|
|
if (isCreate) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
LOG.info("File " + file + " is created.");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String readJsonFile(String fileName) {
|
|
|
|
|
|
String jsonStr = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
File jsonFile = new File(fileName);
|
|
|
|
|
|
FileReader fileReader = new FileReader(jsonFile);
|
|
|
|
|
|
|
|
|
|
|
|
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
|
|
|
|
|
|
int ch = 0;
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
while ((ch = reader.read()) != -1) {
|
|
|
|
|
|
sb.append((char) ch);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fileReader.close();
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
jsonStr = sb.toString();
|
|
|
|
|
|
return jsonStr;
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static String getFileName(File file) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
String[] tmp = file.toString().split("/");
|
2023-01-29 16:36:18 +08:00
|
|
|
|
String fileName = tmp[tmp.length - 1];
|
2023-01-10 16:02:56 +08:00
|
|
|
|
return fileName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static void writerClose(OutputStreamWriter outWriter, OutputStream outStream) throws IOException {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
assert outWriter != null;
|
|
|
|
|
|
outWriter.close();
|
|
|
|
|
|
outStream.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static void readerClose(BufferedReader bufferedReader, InputStreamReader inputStreamReader) throws IOException {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
assert inputStreamReader != null;
|
|
|
|
|
|
bufferedReader.close();
|
|
|
|
|
|
inputStreamReader.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//执行cmd命令,获取返回结果
|
|
|
|
|
|
public static String execCMD(String command) {
|
2023-01-29 16:36:18 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2023-01-10 16:02:56 +08:00
|
|
|
|
try {
|
2023-01-29 16:36:18 +08:00
|
|
|
|
Process process = Runtime.getRuntime().exec(command);
|
|
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
2023-01-10 16:02:56 +08:00
|
|
|
|
String line;
|
2023-01-29 16:36:18 +08:00
|
|
|
|
while ((line = bufferedReader.readLine()) != null) {
|
|
|
|
|
|
sb.append(line + "\n");
|
2023-01-10 16:02:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
return e.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
return sb.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-29 16:36:18 +08:00
|
|
|
|
public static Long getFileLineNum(File file) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
Long num = 0L;
|
2023-01-29 16:36:18 +08:00
|
|
|
|
if (!file.exists()) {
|
2023-01-10 16:02:56 +08:00
|
|
|
|
LOG.error("File not exist: " + file.toString());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
String res = FileUtils.execCMD("wc -l " + file.toString());
|
|
|
|
|
|
num = Long.parseLong(res.trim().split(" ")[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return num;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int getMaxLength(String filePath) {
|
|
|
|
|
|
int lengthDomain = 0;
|
|
|
|
|
|
try {
|
|
|
|
|
|
String encoding = "UTF-8";
|
|
|
|
|
|
File file = new File(filePath);
|
|
|
|
|
|
if (file.isFile() && file.exists()) {
|
|
|
|
|
|
InputStreamReader read = new InputStreamReader(
|
|
|
|
|
|
new FileInputStream(file), encoding);
|
|
|
|
|
|
BufferedReader bufferedReader = new BufferedReader(read);
|
|
|
|
|
|
String lineTxt = null;
|
|
|
|
|
|
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
|
|
|
|
String[] split = lineTxt.split("\\.");
|
|
|
|
|
|
if (split.length > lengthDomain) {
|
|
|
|
|
|
lengthDomain = split.length;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
read.close();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
LOG.error("FilePath is wrong--->{" + filePath + "}<---");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
LOG.error("Get filePathData error--->{" + e + "}<---");
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return lengthDomain;
|
|
|
|
|
|
}
|
2023-01-29 16:36:18 +08:00
|
|
|
|
|
|
|
|
|
|
public static String formatPath(String path) {
|
|
|
|
|
|
path = path.replace("\\\\", "");
|
|
|
|
|
|
path = path.replace("//", "");
|
|
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-01-10 16:02:56 +08:00
|
|
|
|
}
|