文章目录
-
- 1、文件切片
-
- [1.1、File 切片成 N 个保存到本地](#1.1、File 切片成 N 个保存到本地)
- [1.2、MultipartFile 切成 N 个 InputStream](#1.2、MultipartFile 切成 N 个 InputStream)
- 2、目录中的文件读取
1、文件切片
1.1、File 切片成 N 个保存到本地
java
/**
* 读取本地文件 sourceFile , 切成 chunkSize 个文件
* @param sourceFile 源文件
* @param splitFilePath 切片后要保存的目录
* @param chunkSize 切片数量
*/
public static void fileChunking(String sourceFile, String splitFilePath, int chunkSize) {
try (FileInputStream fis = new FileInputStream(sourceFile)) {
byte[] buffer = new byte[chunkSize];
int bytesRead;
int chunkNumber = 1;
while ((bytesRead = fis.read(buffer)) > 0) {
String chunkFileName = splitFilePath + "chunking" + chunkNumber + ".txt";
try (FileOutputStream fos = new FileOutputStream(chunkFileName)) {
fos.write(buffer, 0, bytesRead);
}
chunkNumber++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
使用示例
java
fileChunking("D:\\1.txt", "D:\\chunking", 3);
1.2、MultipartFile 切成 N 个 InputStream
将 前端输入进来的文件 分成N个输入流
java
/**
* 将 前端输入进来的文件 分成N个输入流
* @param file 前端输入进来的文件
* @return 输出流List
*/
public static List<InputStream> splitFile(MultipartFile file) {
try {
int chunkSize = 3; // 每个分片的大小,这里设置为1MB,可以根据需要进行调整
byte[] fileBytes = file.getBytes();
int totalChunks = (int) Math.ceil((double) fileBytes.length / chunkSize);
List<InputStream> chunkInputStreams = new ArrayList<>();
for (int i = 0; i < totalChunks; i++) {
int offset = i * chunkSize;
int chunkLength = Math.min(chunkSize, fileBytes.length - offset);
byte[] chunkBytes = new byte[chunkLength];
System.arraycopy(fileBytes, offset, chunkBytes, 0, chunkLength);
InputStream chunkInputStream = new ByteArrayInputStream(chunkBytes);
chunkInputStreams.add(chunkInputStream);
}
return chunkInputStreams;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// 使用示例:List<InputStream> inputStreams = splitFile(file);
将输出流Lits保存为多个文件
java
/**
* 将多个输出流保存为多个文件
* @param inputStreams 输出流List
* @param outputDirectory 要保存文件的目录
*/
public static void saveToMultipleFiles(List<InputStream> inputStreams, String outputDirectory) {
File directory = new File(outputDirectory);
if (!directory.exists()) {
directory.mkdirs();
}
try {
for (int i = 0; i < inputStreams.size(); i++) {
String filePath = outputDirectory + File.separator + "file" + (i + 1) + ".txt";
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
InputStream inputStream = inputStreams.get(i);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 使用示例: saveToMultipleFiles(inputStreams, "D:\\test\\");
2、目录中的文件读取
读取一个目录下所有txt后缀的文件,按文件内容输出为List<List>
java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class FileProcessor {
public static void main(String[] args) throws IOException {
String directoryPath = "/path/to/directory"; // 替换为实际的目录路径
List<List<String>> fileContents = readTxtFiles(directoryPath);
fileContents.forEach(System.out::println);
}
public static List<List<String>> readTxtFiles(String directoryPath) throws IOException {
List<Path> txtFiles = Files.walk(Paths.get(directoryPath))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".txt"))
.collect(Collectors.toList());
List<List<String>> fileContents = new ArrayList<>();
for (Path txtFile : txtFiles) {
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(txtFile.toFile()))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
}
fileContents.add(lines);
}
return fileContents;
}
}