说明
文件操作工具类
代码
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
@Slf4j
public class FileUtil {
private static final String EMPTY_STRING = "";
/**
* 读取文件
*
* @param filePath 文件路径
* @return 文件内容
*/
public static String read(String filePath) {
if (filePath == null) {
return EMPTY_STRING;
}
File file = new File(filePath);
return read(file, StandardCharsets.UTF_8);
}
/**
* 读取文件
*
* @param file 文件
* @return 文件内容
*/
public static String read(File file) {
return read(file, StandardCharsets.UTF_8);
}
/**
* 读取文件
*
* @param filePath 文件路径
* @param charset 文件字符编码
* @return 文件内容
*/
public static String read(String filePath, Charset charset) {
if (filePath == null) {
return EMPTY_STRING;
}
File file = new File(filePath);
return read(file, charset);
}
/**
* 读取文件
*
* @param file 文件
* @param charset 文件字符编码
* @return 文件内容
*/
public static String read(File file, Charset charset) {
Objects.requireNonNull(charset, "charset can't be null!");
if (file == null || !file.exists()) {
return EMPTY_STRING;
}
StringBuilder sb = new StringBuilder();
try (FileReader fr = new FileReader(file, charset);
BufferedReader br = new BufferedReader(fr)) {
String tmp;
while ((tmp = br.readLine()) != null) {
sb.append(tmp).append(System.lineSeparator());
}
} catch (IOException exception) {
log.error(exception.getMessage());
}
return sb.toString();
}
/**
* 写入文件
*
* @param context 文件内容
* @param filePath 文件路径
*/
public static void write(String context, String filePath) {
write(context, filePath, StandardCharsets.UTF_8);
}
/**
* 写入文件
*
* @param context 文件内容
* @param file 文件
*/
public static void write(String context, File file) {
write(context, file, StandardCharsets.UTF_8);
}
/**
* 写入文件
*
* @param context 文件内容
* @param filePath 文件路径
* @param charset 文件字符编码
*/
public static void write(String context, String filePath, Charset charset) {
Objects.requireNonNull(filePath, "filePath can't be null!");
write(context, new File(filePath), charset);
}
/**
* 写入文件
*
* @param context 文件内容
* @param file 文件
* @param charset 文件字符编码
*/
public static void write(String context, File file, Charset charset) {
Objects.requireNonNull(charset, "charset can't be null!");
Objects.requireNonNull(file, "file can't be null!");
if (context == null) {
context = EMPTY_STRING;
}
try (FileWriter fw = new FileWriter(file, charset)) {
fw.write(context);
fw.flush();
} catch (IOException exception) {
log.error(exception.getMessage());
}
}
/**
* 读取classpath下的文件(文件路径需以'/'开头)
*
* @param filePath 文件路径
* @return 文件内容
*/
public static String readClassPath(String filePath) {
return readClassPath(filePath, StandardCharsets.UTF_8);
}
/**
* 读取classpath下的文件(文件路径需以'/'开头)
*
* @param filePath 文件路径
* @param charset 文件字符编码
* @return 文件内容
*/
public static String readClassPath(String filePath, Charset charset) {
Objects.requireNonNull(charset, "charset can't be null!");
try (InputStream is = FileUtil.class.getResourceAsStream(filePath);) {
return new String(is.readAllBytes(), charset);
} catch (IOException exception) {
log.error(exception.getMessage());
}
return EMPTY_STRING;
}
}

6万+

被折叠的 条评论
为什么被折叠?



