


Java uses IO streams to implement splitting and merging large files with detailed examples
Java 使用IO流实现大文件的分割与合并
文件分割应该算一个比较实用的功能,举例子说明吧比如说:你有一个3G的文件要从一台电脑Copy到另一台电脑, 但是你的存储设备(比如SD卡)只有1G ,这个时候就可以把这个文件切割成3个1G的文件 ,分开复制, 最后把三个文件合并, 这样就解决问题了 ;再比如说, 你有一个上百M的文件要上传到FTP ,但是这个FTP限制你单个文件不能超过10M 这时候也可以用文件分割的办法解决问题。既然分割了,那么在我们再次使用的时候就需要合并了,今天我们就通过Java代码实现文件分裂与合并的能。
现在通过演示我本机的一个文件来演示,文件目录为:E:\eclipse-jee-juno-win32.zip(今天就把大家痛恨的eclipse好好玩一下):
下图为分割前的情况:
分割后的情况为:
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中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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. ...

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

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...

Start Spring using IntelliJIDEAUltimate version...

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...

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 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...

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...
