java 输入流可以合并吗_HOW2J Java 文件输入输出流,合并与拆分

//需要所指目录下确有一个文件供拆分

//多余的另成一个文件

package File;

import java.util.*;

import java.io.*;

public class TestStrem{

public static void main(String[] args) {

int eachSize=100*1024//100k;

File scrFile = new File("d:/javaha/gys.txt");

splitFile(scrFile,eachSize);

}

public static void splitFile(File scrFile,int eachSize) {

if(0==scrFile.length())

throw new RuntimeException("文件长度为0,不可拆分");

byte[] fileConent = new byte[(int)scrFile.length()];

try {

FileInputStream fis =new FileInputStream(scrFile);

fis.read(fileConent);

fis.close();

}

catch(IOException e) {

e.printStackTrace();

}

int fileNumber;

if(fileConent.length % eachSize==0)

fileNumber =(int)(fileConent.length / eachSize);

else

fileNumber =(int)(fileConent.length / eachSize )+ 1;

for(int i=0;i

String eachFilename=scrFile.getName()+"-"+ i;

File eachFile =new File(scrFile.getParent(),eachFilename);

byte[] eachContent;

if(i!=fileNumber-1)

eachContent =Arrays.copyOfRange(fileConent, eachSize*i , eachSize);

else

eachContent =Arrays.copyOfRange(fileConent, eachSize*i , fileConent.length);

try {

FileOutputStream fos =new FileOutputStream(eachFile);

fos.write(eachContent);

fos.close();

System.out.format("输出子文件%s,其大小是%d字节",eachFile.getAbsolutePath(),eachFile.length());

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

//合并,

package stream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.security.auth.DestroyFailedException;

public class TestStream {

public static void main(String[] args) {

murgeFile("d:/", "eclipse.exe");

}

/**

* 合并的思路,就是从eclipse.exe-0开始,读取到一个文件,就开始写出到 eclipse.exe中,直到没有文件可以读

* @param folder

* 需要合并的文件所处于的目录

* @param fileName

* 需要合并的文件的名称

* @throws FileNotFoundException

*/

private static void murgeFile(String folder, String fileName) {

try {

// 合并的目标文件

File destFile = new File(folder, fileName);

FileOutputStream fos = new FileOutputStream(destFile);

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写出到目标文件中%n",eachFile);

}

fos.close();

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

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值