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中文网!

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多态性、Lambda表达式、StreamsAPI、泛型和异常处理。1.多态性让不同类的对象可以作为共同基类的对象使用。2.Lambda表达式使代码更简洁,特别适合处理集合和流。3.StreamsAPI高效处理大数据集,支持声明式操作。4.泛型提供类型安全和重用性,编译时捕获类型错误。5.异常处理帮助优雅处理错误,编写可靠软件。

java'stopfeatureSnificallyEnhanceItsperFormanCeanDscalability.1)对象 - 方向 - incipleslike-polymormormormormormormormormormormormormorableablefleandibleandscalablecode.2)garbageCollectionAutoctionAutoctionAutoctionAutoctionAutoctionautomorymanatesmemorymanateMmanateMmanateMmanagementButCancausElatenceiss.3)

JVM的核心组件包括ClassLoader、RuntimeDataArea和ExecutionEngine。1)ClassLoader负责加载、链接和初始化类和接口。2)RuntimeDataArea包含MethodArea、Heap、Stack、PCRegister和NativeMethodStacks。3)ExecutionEngine由Interpreter、JITCompiler和GarbageCollector组成,负责bytecode的执行和优化。

Java'ssafetyandsecurityarebolsteredby:1)strongtyping,whichpreventstype-relatederrors;2)automaticmemorymanagementviagarbagecollection,reducingmemory-relatedvulnerabilities;3)sandboxing,isolatingcodefromthesystem;and4)robustexceptionhandling,ensuringgr

javaoffersseveralkeyfeaturesthatenhancecodingskills:1)对象 - 方向 - 方向上的贝利奥洛夫夫人 - 启动worldentities

thejvmisacrucialcomponentthatrunsjavacodebytranslatingitolachine特定建筑,影响性能,安全性和便携性。1)theclassloaderloader,links andinitializesClasses.2)executionEccutionEngineExecutionEngineExecutionEngineExecuteByteCuteByteCuteByteCuteBytecuteBytecuteByteCuteByteCuteByteCuteBytecuteByteCodeNinstRonctientions.3)Memo.3)Memo


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中