java文件切割与合并

在生活中,我们也许会遇到想要拷贝文件到U盘,但文件太大,U盘空间放不下。这时,我们就可以利用我们学过的java中输入输出流来解决这个问题。

以下是我用输入输出流写的一个分割与合并文件的小程序,菜鸟刚学习java,见笑了。

package inout;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Cutting {
/**

* @param sourceFilePath  //被合并文件的路径
* @param destFilePath   //合并后的文件的路径和名称
* @throws Exception
*/
public static void combine(String sourceFilePath, String destFilePath) throws Exception {
//验证被合并的文件目录是否存在,若不存在就停止合并
if (sourceFilePath == null) {
System.out.println("被合并的文件目录不存在,停止合并");
return;
}
//验证切割后的文件存储路径是否输入,若没有输入则停止合并
if (destFilePath == null) {
System.out.println("切割后的文件存储路径不存在,停止合并");
return;
}
//验证切割后的文件存储路径是否存在,不存在则创建
String destPath1 = destFilePath.substring(0, destFilePath.lastIndexOf("\\") + 1);
File destFilePath1 = new File(destPath1);
if (!destFilePath1.exists()) {
destFilePath1.mkdirs();
}
//准备合并后的文件
File destFile = new File(destFilePath);
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);


File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
System.out.println("源文件不存在");
return;
}
File[] file = sourceFile.listFiles();
List<File> list = new ArrayList<File>();
//开始合并文件
for (int i = 0; i < file.length; i++) {
if (file[i].getAbsolutePath().toLowerCase().endsWith(".part")) {
list.add(file[i]);
}
}
for (int i = 0; i < list.size(); i++) {
File f = list.get(i);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
int temp;
while ((temp = bis.read()) != -1) {
bos.write(temp);
}
bis.close();
   fis.close();


}
bos.close();
fos.close();

}
    /**
     * 
     * @param Path    //被切割的文件路径和文件名
     * @param size    //每份大小,单位字节
     * @throws Exception
     */
public static void spilt(String sourceFilePath, String destFilePath, long size) throws Exception {
//验证被切割的文件是否存在,若不停止则停止切割
if (sourceFilePath == null) {
System.out.println("被合并的文件目录不存在,停止合并");
return;
}
//验证存储切割后的文件路径是否存在,不存在则创建
if (!destFilePath.endsWith("\\")) {
destFilePath = destFilePath + "\\";
}
File folder = new File(destFilePath);
if (!folder.exists()) {
folder.mkdirs();
}
//找到被切割的文件
File file = new File(sourceFilePath);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
        //获取被切割文件的大小
long s = file.length();
//计算要切割成几份
long jifen = s / size;
//计算最后一份的大小
long mod = s % size;
//开始切前面的份数
int i = 0;
for (; i < jifen; i++) {
File dest = new File(destFilePath + (i + 100) + ".part");
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(fos);
            //开始从源中读取,然后写入目的
int temp;
int j = 0;
while (j < size && (temp = bis.read()) != -1) {
bos.write(temp);
j++;
}
bos.close();
fos.close();


}
//开始切割最后一份
if (mod > 0) {
File dest = new File(destFilePath + (i + 100) + ".part");
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int temp;
while ((temp = bis.read()) != -1) {
bos.write(temp);
}
bos.close();
fos.close();
}
bis.close();
fis.close();
}


public static void main(String[] args) {


System.out.println("*****************************");
System.out.println("      文件切割合并工具        ");
System.out.println("           1.切割             ");
System.out.println("           2.合并             ");
System.out.println("*****************************");
Scanner scanner = new Scanner(System.in);
try {
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入要切割的文件名,例如:c:\\tools\\tom.exe");
String sourceFilePath1 = scanner.next();
System.out.println("请输入切割后的文件存储路径,例如:c\\cjc\\");
String destFilePath1 = scanner.next();
System.out.println("正在切割,请等待...");
spilt(sourceFilePath1,destFilePath1,(1024*1024));
System.out.println("切割完毕");
break;
case 2:
System.out.println("请输入要合并的文件名,例如:c\\cjc\\");
String sourceFilePath2 = scanner.next();
System.out.println("请输入合并后的文件存储路径,例如:c:\\tools\\tom.exe");
String destFilePath2 = scanner.next();
System.out.println("正在合并,请等待...");
combine(sourceFilePath2,destFilePath2);
System.out.println("合并完毕");
break;
}


} catch (Exception 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、付费专栏及课程。

余额充值