java 小文件合并代码
时间: 2023-04-06 16:01:32 浏览: 90
Java 文件合并
4星 · 用户满意度95%
以下是 Java 合并小文件的代码示例:
```java
import java.io.*;
public class MergeFiles {
public static void main(String[] args) throws IOException {
String dirPath = "D:/test/"; // 文件夹路径
String mergeFileName = "merged.txt"; // 合并后的文件名
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files == null || files.length == 0) {
System.out.println("文件夹中没有文件!");
return;
}
File mergeFile = new File(dirPath + mergeFileName);
if (mergeFile.exists()) {
mergeFile.delete();
}
mergeFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(mergeFile));
for (File file : files) {
if (file.isFile()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
reader.close();
}
}
writer.close();
System.out.println("文件合并完成!");
}
}
```
阅读全文