自己创建文件分割合并工具(JAVA)

最近在外边工作,上网不用像学校那样计流量,于是以前看过的经典电视剧又可以下载下来重新温习一遍,但是又不可能在工作地方看,就只有永革1G的优盘每天往自己的电脑上Copy一点,留着以后看。不过,这几天下载的东西每集140M左右,U盘装了6集,还有100M的空间,装一集不够。我又不想去找个文件分割器,反正顺便学学JAVA,练练手,就随便写了个文件分割合并的工具,用上了昨天提到的Properties。
这个分割工具很简单,因为我是自己用,不用考虑什么客户不客户的,自己能够保证每次正确输入,错了能够马上改一下,重新编译,所以做的就简单了。以下是分割的工具代码。FileSplit.java
import java.io.*;
public class FileSplit
{
 public static void main(String[] args) throws Exception
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String fn = null;
  System.out.print("请输入文件路径全名:");
  fn = br.readLine();
  System.out.print("请输入分割后的文件基础名称:");
  String fna = br.readLine();
  System.out.print("请输入分割的块数:");
  String c = br.readLine();
  int count = Integer.parseInt(c);
  File f = new File(fn);
  RandomAccessFile file = new RandomAccessFile(f,"r");
  int ec = (int)file.length()/count;
  FileOutputStream fos = null;
  for (int i=0 ;i<count ;i++ )
  {
   int pos = ec * i;
   byte [] b = new byte[ec];
   if(i == count-1) b = new byte[(int)file.length()-i*ec];
   file.seek(pos);
   file.read(b);
   String nfn = fna + i + ".fs";
   fos = new FileOutputStream(nfn);
   fos.write(b);
   fos.close();
   System.out.println("文件:"+nfn+" 创建成功。");
  }
  file.close();
  String propertiesFile = fna + ".fsp";
  BufferedWriter bw = new BufferedWriter(new FileWriter(propertiesFile));
  bw.write("originalFileName=" + f.getName());
  bw.newLine();
  bw.write("newFileFoundationName=" + fna);
  bw.newLine();
  bw.write("fileSplitCount=" + count);
  bw.newLine();
  bw.close();
  System.out.println("文件分割完成!");
 }
}
这里用上了昨天所说的Properties,保存了源分割文件的名称,分割后文件的基础名称,以及分割的块数(几乎每个文件的大小差不多)。不过由于合并的时候写上.properties很麻烦,我用了一个简短的替代名fsp,全称叫FileSplitProperties。以下是对他的操作文件,FSProperty.java:
import java.io.*;
import java.util.*;
public class FSProperty
{
 String propertiesFile;
 public FSProperty(String pf)
 {
  this.propertiesFile = pf;
 }
 public ArrayList getPro()
 {
  ArrayList list = new ArrayList();
  try
  {
   Properties p = new Properties();
   InputStream is = getClass().getResourceAsStream(propertiesFile);
   p.load(is);
   String a = p.getProperty("originalFileName");
   a = new String(a.getBytes("ISO8859-1"),"GBK");
   list.add(a);
   a = p.getProperty("newFileFoundationName");
   a = new String(a.getBytes("ISO8859-1"),"GBK");
   list.add(a);
   a = p.getProperty("fileSplitCount");
   //a = new String(a.getBytes("ISO8859-1"),"GBK");
   list.add(a);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  return list;
 }
}
这里注意一点,我们很多时候会用到中文的文件名,使用Properties读取出来的话,会变成乱码,结果就会出现找不到文件的问题,总不可能每次都是使用“另存为”去转码,于是就使用在程序中自动转码的方法,节省很多功夫。
然后就是合并文件的代码,FileBuild.java:
import java.io.*;
import java.util.*;
public class FileBuild
{
 public static void main(String[] args) throws Exception
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("请输入属性文件路径全称:");
  String pfn = br.readLine();
  System.out.print("请输入合成后文件名称:");
  String nfn = br.readLine();
  
  FSProperty fsp = new FSProperty(pfn);
  ArrayList list = fsp.getPro();
  String ofn = (String)list.get(0);
  String nffn = (String)list.get(1);
  String fsc = (String)list.get(2);
  if (nfn == null || nfn.equals(""))
  {
   nfn = ofn;
  }
  else if (ofn.lastIndexOf(".")!=-1 && (nfn.lastIndexOf(".")==-1 || !nfn.substring(nfn.lastIndexOf(".")+1).equals(ofn.substring(ofn.lastIndexOf(".")+1))))
  {
   nfn += ofn.substring(ofn.lastIndexOf("."));
  }

  int count = Integer.parseInt(fsc);


  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(nfn, true));
  FileInputStream fis = null;
  for (int i=0 ;i<count ;i++ )
  {
   byte [] b = new byte[102400];
   int read;
   String fn = nffn + i + ".fs";
   fis = new FileInputStream(fn);
   while((read = fis.read(b))!=-1)
   {
    bos.write(b, 0, read);
   }
   bos.flush();
   fis.close();
  }
  bos.close();
  System.out.println("文件:"+nfn+" 合成成功。");
 }
}
这里有仅有的一项人性化设计,就是不用输入合并后的文件的正确后缀名,程序会自动添加。但是这里在使用FileOutputStream的时候使用了true参数,如果已经有同名的文件,就会出问题,直接添加到这个文件后边。本来想写一个验证的,嫌麻烦,反正自己用的,能够保证尽量少的错误,即使有错也能够很快改正,不必担心用户体验。自娱自乐就行了。
很简单的东西,就没写注释,相信大家都是很轻松的不屑一顾了。有兴趣的朋友也可以探讨一下。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值