您的位置:首页 > 编程语言 > Java开发

java的IO流----将多个拆分的子文件合并为一个目标文件

2017-01-03 22:12 417 查看
我们可以一边读取文件内容,一边将内容写道目标文件中

package testIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class murgeFile {

public static void main(String[] args) {
murgeFile("F:/test/","test.txt.txt");
}

public static void murgeFile(String folder,String fileName){
try {
File desFile = new File(folder,fileName);
FileOutputStream fos = new FileOutputStream(desFile);
int index=0;

//读取每一个子文件
while(true){
File eachFile = new File(folder,fileName+"-"+index++);
if(!eachFile.exists()){
break;
}
//将一个文件读取到内存中
FileInputStream fis=new FileInputStream(eachFile);
byte[] eachContent = new byte[(int) eachFile.length()];
fis.read(eachContent);
fis.close();

fos.write(eachContent);
fos.flush();
System.out.printf("把子文件 %s写出到目标文件 中",eachFile);
}

fos.close();
System.out.printf("最后目标文件的大小:%d字节:",desFile.length());

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: