import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test_5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
murgeFile("**","**");
}
/*
*合并文件
*一边读取子文件的内容,一边写出到目标文件,直到没有文件可以读
* @param folder
* 需要合并的文件所处于的目录
* @param fileName
* 需要合并的文件的名称
*/
private static void murgeFile(String folder, String fileName) {
// TODO Auto-generated method stub
try {
//合并的目标文件
File file=new File(folder,fileName);
FileOutputStream fos=new FileOutputStream(file);
int index=0;
while(true){
//子文件
File file2=new File(folder,fileName+"_"+index++);
if (!file2.exists()) {
break;
}
//读取字文件内容,把子文件写出去
FileInputStream fis=new FileInputStream(file2);
byte []b=new byte[(int) file2.length()];
fis.read(b);
fis.close();
//写出子文件
fos.write(b);
fos.flush();
System.out.println(file2);
}
fos.close();
System.out.println(file.length());
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test_5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
murgeFile("**","**");
}
/*
*合并文件
*一边读取子文件的内容,一边写出到目标文件,直到没有文件可以读
* @param folder
* 需要合并的文件所处于的目录
* @param fileName
* 需要合并的文件的名称
*/
private static void murgeFile(String folder, String fileName) {
// TODO Auto-generated method stub
try {
//合并的目标文件
File file=new File(folder,fileName);
FileOutputStream fos=new FileOutputStream(file);
int index=0;
while(true){
//子文件
File file2=new File(folder,fileName+"_"+index++);
if (!file2.exists()) {
break;
}
//读取字文件内容,把子文件写出去
FileInputStream fis=new FileInputStream(file2);
byte []b=new byte[(int) file2.length()];
fis.read(b);
fis.close();
//写出子文件
fos.write(b);
fos.flush();
System.out.println(file2);
}
fos.close();
System.out.println(file.length());
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}