- 一、FileUtil.java
- 二、FileUtilTest.java
GitHub: link. 欢迎star
一、FileUtil.java注意:本篇博客风格(不多比比就是撸代码!!!)
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
@Slf4j
public class FileUtil {
private static Path rootFilePath;
static {
try {
String osName = System.getProperty("os.name");
log.info("osName:{}", osName);
String fileRootPath; //文件存放目录
if (osName.toLowerCase().contains("windows")) {
fileRootPath = "D:\apps\file"; // 指定windows系统下RocksDB文件目录
} else {
fileRootPath = "/usr/local/apps/file"; // 指定linux系统下RocksDB文件目录
}
rootFilePath = Paths.get(fileRootPath);
if (!Files.exists(rootFilePath)) {
Files.createDirectories(rootFilePath);
}
log.info("rootFilePath:{}", rootFilePath.toAbsolutePath());
} catch (Exception e) {
log.error("FileUtil init failure!! error:{}", e.getMessage());
e.printStackTrace();
}
}
public static File createFile(String... fileNamePath) throws IOException {
Path path = Paths.get(rootFilePath.toAbsolutePath().toString(), fileNamePath);
if (!Files.exists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.deleteIfExists(path);
Files.createFile(path);
log.info("createFile success!! --文件-> [{}]", path.toAbsolutePath().toString());
return path.toFile();
}
public static String save(MultipartFile multipartFile, String... fileNamePath) throws IOException {
Path path = Paths.get(rootFilePath.toAbsolutePath().toString(), fileNamePath);
if (!Files.exists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.copy(multipartFile.getInputStream(), path);
log.info("save success!! --文件-> [{}]", path.toAbsolutePath().toString());
return path.toAbsolutePath().toString();
}
public static String copy(String fromFilePath, String... fileNamePath) throws IOException {
Path path = Paths.get(rootFilePath.toAbsolutePath().toString(), fileNamePath);
if (!Files.exists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.copy(new File(fromFilePath).toPath(), path);
log.info("copy success!! --文件-> [{}]", path.toAbsolutePath().toString());
return path.toAbsolutePath().toString();
}
public static File createFileWithContent(String fileName, List lines) throws IOException {
File file = createFile(fileName);
try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
for (String line : lines) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
log.info("createFileWithContent success!! --文件-> [{}]", file.getAbsolutePath());
return file;
}
}
public static void appendContentToFile(File file, List lines) throws IOException {
synchronized (FileUtil.class) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) {
for (String line : lines) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
}
}
}
public static void readFileContentByLine(String filePath, Consumer contentReader) throws Exception {
String charset = charset(filePath);
try (FileInputStream inputStream = new FileInputStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
contentReader.accept(bufferedReader);
}
}
public static String readSchemaWithFirstLine(String filePath) throws Exception {
AtomicReference schema = new AtomicReference<>("");
readFileContentByLine(filePath, bufferedReader -> {
try {
schema.set(bufferedReader.readLine());
} catch (Exception e) {
schema.set("");
}
});
log.info("readSchemaWithFirstLine success!! --文件-> [{}] schema={}", filePath, schema);
return schema.get();
}
public static int readDataCount(String filePath) throws Exception {
int linesCount = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
while (reader.readLine() != null) {
linesCount++;
}
}
log.info("readDataCount success!! --文件-> [{}] linesCount={}", filePath, linesCount);
return linesCount;
}
public static String charset(String path) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
bis.close();
return charset; // 文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; // 文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE"; // 文件编码为 Unicode big endian
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8"; // 文件编码为 UTF-8
checked = true;
}
bis.reset();
if (!checked) {
while ((read = bis.read()) != -1) {
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 > read || read > 0xBF) {
break; // 双字节 (0xC0 - 0xDF),(0x80 - 0xBF),也可能在GB编码内
}
} else if (0xE0 <= read) { // 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
}
}
break;
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
log.info("--文件-> [{}] 采用的字符集为: [{}]", path, charset);
return charset;
}
}
二、FileUtilTest.java
import com.alibaba.fastjson.JSONObject;
import com.andon.springbootutil.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@Slf4j
public class FileUtilTest {
@Test
public void appendContentToFile() throws IOException {
File file = FileUtil.createFile("createFile.csv");
List header = Collections.singletonList("id,name");
FileUtil.appendContentToFile(file, header);
int row = 8;
List> futures = new ArrayList<>(row);
for (int i = 0; i < row; i++) {
int finalI = i;
CompletableFuture future = CompletableFuture.runAsync(() -> {
try {
List lines = Collections.singletonList(finalI + "," + Thread.currentThread().getName());
FileUtil.appendContentToFile(file, lines);
} catch (IOException e) {
e.printStackTrace();
}
});
futures.add(future);
}
CompletableFuture completableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
completableFuture.join();
}
@Test
public void readFileContentByLine() throws Exception {
String filePath = "D:\apps\file\createFile.csv";
int batchSize = 5;
List lines = new ArrayList<>(batchSize);
FileUtil.readFileContentByLine(filePath, (bufferedReader) -> {
String line;
while (true) {
try {
if ((line = bufferedReader.readLine()) == null) break;
} catch (Exception e) {
break;
}
lines.add(line);
if (lines.size() == batchSize) {
log.info("lines.size={} lines:{}", lines.size(), JSONObject.toJSONString(lines));
lines.clear();
}
}
if (!lines.isEmpty()) {
log.info("lines.size={} lines:{}", lines.size(), JSONObject.toJSONString(lines));
lines.clear();
}
});
}
@Test
public void readSchemaWithFirstLine() throws Exception {
String filePath = "D:\apps\file\createFile.csv";
String schema = FileUtil.readSchemaWithFirstLine(filePath);
}
@Test
public void readDataCount() throws Exception {
String filePath = "D:\apps\file\createFile.csv";
int count = FileUtil.readDataCount(filePath);
}
@Test
public void createFileWithContent() throws IOException {
int row = 8;
List lines = new ArrayList<>(row);
for (int i = 0; i < row; i++) {
lines.add(i + "," + UUID.randomUUID().toString());
}
File fileWithContent = FileUtil.createFileWithContent("createFileWithContent.tmp", lines);
}
@Test
public void copy() throws IOException {
String file = "D:\apps\file\createFile.csv";
String copy = FileUtil.copy(file, UUID.randomUUID().toString(), "copy.tmp");
}
@Test
public void createFile() throws IOException {
FileUtil.createFile(UUID.randomUUID().toString(), "HelloWorld.tmp");
}
}
GitHub: link. 欢迎star



