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);
}
}
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);
}
}