java 合并文件夹下多个文件

package test.com.whty.platform.modules.interfaces;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Test {
private static String filePath="d:\\1\\拆单文件合并\\";
 static File rootFile=new File(filePath);
 static File[] listFile=rootFile.listFiles();

public static void merge(File[] fileNames, String TargetFileName)
throws Exception {
File fin = null;
// 构建文件输出流
File fout = new File(TargetFileName);
FileOutputStream out = new FileOutputStream(fout);
for (int i = 0; i < fileNames.length; i++) {
// 打开文件输入流
fin = fileNames[i];
FileInputStream in = new FileInputStream(fin);
// 从输入流中读取数据,并写入到文件数出流中
int c=-1;
byte[] data=new byte[in.available()];
in.read(data);
out.write(data);
// while ((c = in.read()) != -1) {
// out.write(c);
// }
out.write("\r\n".getBytes());
in.close();
}
out.close();
System.out.println("合并文件" + TargetFileName + "中的内容如下:");
}
public static void readFileMessage(String fileName) throws IOException {// 将合成的文件中的内容读出
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String string = null;
// 按行读取内容,直到读入null则表示读取文件结束
while ((string = reader.readLine()) != null) {
System.out.println(string);
}
reader.close();
} finally {
if (reader != null) {

reader.close();

}
}
}
public static void main(final String[] args) throws Exception {
// 合并文件

String newFileName = "mergeSPLIT2.txt";
Test.merge(listFile, newFileName);
}






}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值