Home Java javaTutorial 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

Jan 11, 2017 pm 02:52 PM

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();
   }
  }
 }
}
Copy after login

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("文件合并完成!");
}
Copy after login

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

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

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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1263
29
C# Tutorial
1237
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles