241 lines
7.1 KiB
Java
241 lines
7.1 KiB
Java
package com.arpa.hndahesudintocctmsdriver.util;
|
|
|
|
import com.arpa.hndahesudintocctmsdriver.FileUtils;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
|
|
/**
|
|
* @ClassName FileIOUtils
|
|
* @Author john
|
|
* @Date 2024/3/29 09:50
|
|
* @Description TODO
|
|
*/
|
|
public class FileIOUtils {
|
|
|
|
private FileIOUtils() {
|
|
throw new UnsupportedOperationException("u can't instantiate me...");
|
|
}
|
|
|
|
private static final String LINE_SEP = System.getProperty("line.separator");
|
|
|
|
/**
|
|
* 将输入流写入文件
|
|
*
|
|
* @param filePath 路径
|
|
* @param is 输入流
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromIS(String filePath, final InputStream is) {
|
|
return writeFileFromIS(FileUtils.getFileByPath(filePath), is, false);
|
|
}
|
|
|
|
/**
|
|
* 将输入流写入文件
|
|
*
|
|
* @param filePath 路径
|
|
* @param is 输入流
|
|
* @param append 是否追加在文件末
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromIS(String filePath, final InputStream is, boolean append) {
|
|
return writeFileFromIS(FileUtils.getFileByPath(filePath), is, append);
|
|
}
|
|
|
|
/**
|
|
* 将输入流写入文件
|
|
*
|
|
* @param file 文件
|
|
* @param is 输入流
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromIS(File file, final InputStream is) {
|
|
return writeFileFromIS(file, is, false);
|
|
}
|
|
|
|
/**
|
|
* 将输入流写入文件
|
|
*
|
|
* @param file 文件
|
|
* @param is 输入流
|
|
* @param append 是否追加在文件末
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromIS(File file, final InputStream is, boolean append) {
|
|
if (!FileUtils.createOrExistsFile(file) || is == null) return false;
|
|
OutputStream os = null;
|
|
try {
|
|
os = new BufferedOutputStream(new FileOutputStream(file, append));
|
|
byte data[] = new byte[1024];
|
|
int len;
|
|
while ((len = is.read(data, 0, 1024)) != -1) {
|
|
os.write(data, 0, len);
|
|
}
|
|
return true;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
} finally {
|
|
try {
|
|
is.close();
|
|
os.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 将字符串写入文件
|
|
*
|
|
* @param filePath 文件路径
|
|
* @param content 写入内容
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromString(String filePath, String content) {
|
|
return writeFileFromString(FileUtils.getFileByPath(filePath), content, false);
|
|
}
|
|
|
|
/**
|
|
* 将字符串写入文件
|
|
*
|
|
* @param filePath 文件路径
|
|
* @param content 写入内容
|
|
* @param append 是否追加在文件末
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromString(String filePath, String content, boolean append) {
|
|
return writeFileFromString(FileUtils.getFileByPath(filePath), content, append);
|
|
}
|
|
|
|
/**
|
|
* 将字符串写入文件
|
|
*
|
|
* @param file 文件
|
|
* @param content 写入内容
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromString(File file, String content) {
|
|
return writeFileFromString(file, content, false);
|
|
}
|
|
|
|
/**
|
|
* 将字符串写入文件
|
|
*
|
|
* @param file 文件
|
|
* @param content 写入内容
|
|
* @param append 是否追加在文件末
|
|
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
|
|
*/
|
|
public static boolean writeFileFromString(File file, String content, boolean append) {
|
|
if (file == null || content == null) return false;
|
|
if (!FileUtils.createOrExistsFile(file)) return false;
|
|
BufferedWriter bw = null;
|
|
try {
|
|
bw = new BufferedWriter(new FileWriter(file, append));
|
|
bw.write(content);
|
|
bw.newLine();
|
|
return true;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
} finally {
|
|
try {
|
|
bw.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 读取文件到字符串中
|
|
*
|
|
* @param filePath 文件路径
|
|
* @return 字符串
|
|
*/
|
|
public static String readFile2String(String filePath) {
|
|
return readFile2String(FileUtils.getFileByPath(filePath), null);
|
|
}
|
|
|
|
/**
|
|
* 读取文件到字符串中
|
|
*
|
|
* @param filePath 文件路径
|
|
* @param charsetName 编码格式
|
|
* @return 字符串
|
|
*/
|
|
public static String readFile2String(String filePath, String charsetName) {
|
|
return readFile2String(FileUtils.getFileByPath(filePath), charsetName);
|
|
}
|
|
|
|
/**
|
|
* 读取文件到字符串中
|
|
*
|
|
* @param file 文件
|
|
* @return 字符串
|
|
*/
|
|
public static String readFile2String(File file) {
|
|
return readFile2String(file, null);
|
|
}
|
|
|
|
/**
|
|
* 读取文件到字符串中
|
|
*
|
|
* @param file 文件
|
|
* @param charsetName 编码格式
|
|
* @return 字符串
|
|
*/
|
|
public static String readFile2String(File file, String charsetName) {
|
|
if (!FileUtils.isFileExists(file)) return null;
|
|
BufferedReader reader = null;
|
|
try {
|
|
StringBuilder sb = new StringBuilder();
|
|
if (isSpace(charsetName)) {
|
|
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
|
|
} else {
|
|
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
|
|
}
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
sb.append(line).append(LINE_SEP);
|
|
}
|
|
// delete the last line separator
|
|
return sb.delete(sb.length() - LINE_SEP.length(), sb.length()).toString();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
} finally {
|
|
try {
|
|
reader.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private static boolean isSpace(String s) {
|
|
if (s == null) return true;
|
|
for (int i = 0, len = s.length(); i < len; ++i) {
|
|
if (!Character.isWhitespace(s.charAt(i))) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|