Home  >  Article  >  Java  >  Java uses IO streams to implement splitting and merging large files with detailed examples

Java uses IO streams to implement splitting and merging large files with detailed examples

高洛峰
高洛峰Original
2017-01-11 14:52:211625browse

Java 使用IO流实现大文件的分割与合并

文件分割应该算一个比较实用的功能,举例子说明吧比如说:你有一个3G的文件要从一台电脑Copy到另一台电脑, 但是你的存储设备(比如SD卡)只有1G ,这个时候就可以把这个文件切割成3个1G的文件 ,分开复制, 最后把三个文件合并, 这样就解决问题了 ;再比如说, 你有一个上百M的文件要上传到FTP ,但是这个FTP限制你单个文件不能超过10M 这时候也可以用文件分割的办法解决问题。既然分割了,那么在我们再次使用的时候就需要合并了,今天我们就通过Java代码实现文件分裂与合并的能。

现在通过演示我本机的一个文件来演示,文件目录为:E:\eclipse-jee-juno-win32.zip(今天就把大家痛恨的eclipse好好玩一下):

下图为分割前的情况:

Java 使用IO流实现大文件的分割与合并实例详解

分割后的情况为:

Java 使用IO流实现大文件的分割与合并实例详解

Java分割文件的方法:

//文件分割的方法(方法内传入要分割的文件路径以及要分割的份数)
private static void splitFileDemo(File src, int m) {
 if(src.isFile()) {
  //获取文件的总长度
  long l = src.length();
  //获取文件名
  String fileName = src.getName().substring(0, src.getName().indexOf("."));
  //获取文件后缀
  String endName = src.getName().substring(src.getName().lastIndexOf("."));
  System.out.println(endName);
  InputStream in = null;
  try {
   in = new FileInputStream(src);
   for(int i = 1; i <= m; i++) {
    StringBuffer sb = new StringBuffer();
    sb.append(src.getParent()).append("\\").append(fileName)
    .append("_data").append(i).append(endName);
    System.out.println(sb.toString());
    File file2 = new File(sb.toString());
    //创建写文件的输出流
    OutputStream out = new FileOutputStream(file2);
    int len = -1;
    byte[] bytes = new byte[10*1024*1024];
    while((len = in.read(bytes))!=-1) {
     out.write(bytes, 0, len);
     if(file2.length() > (l / m)) {
      break;
     }
    }
    out.close();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  finally {
   try {
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

Java合并文件的方法:

//文件合并的方法(传入要合并的文件路径)
private static void joinFileDemo(String... src) {
 for(int i = 0; i < src.length; i++) {
  File file = new File(src[i]);
  String fileName = file.getName().substring(0, file.getName().indexOf("_"));
  String endName = file.getName().substring(file.getName().lastIndexOf("."));
  StringBuffer sb = new StringBuffer();
  sb.append(file.getParent()).append("\\").append(fileName)
  .append(endName);
  System.out.println(sb.toString());
  try {
   //读取小文件的输入流
   InputStream in = new FileInputStream(file);
   //写入大文件的输出流
   File file2 = new File(sb.toString());
   OutputStream out = new FileOutputStream(file2,true);
   int len = -1;
   byte[] bytes = new byte[10*1024*1024];
   while((len = in.read(bytes))!=-1) {
    out.write(bytes, 0, len);
   }
   out.close();
   in.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 System.out.println("文件合并完成!");
}

写之前觉得挺复杂,写完之后觉得也就那样,大家也可以练练手。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多Java 使用IO流实现大文件的分割与合并实例详解相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn