首页 / JAVA / javase文件切割及融合
javase文件切割及融合
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javase文件切割及融合,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2774字,纯文字阅读大概需要4分钟。
内容图文
1 package com.split; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 8 public class FileSplit { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 // String file = "D:\\arch\\d.jpg"; 13 String file="D:\\arch\\b.txt"; 14 split(file); 15 String s1="D:\\arch\\b.txtpart-0"; 16 String s2="D:\\arch\\b.txtpart-1"; 17 String s3="D:\\arch\\b.txtpart-2"; 18 fuse(s1,s2,s3); 19 20 } 21/** 22 * 文件融合 23 * @param srcFile 24*/ 25publicstaticvoid fuse(String... srcFile ){ 26for (String string : srcFile) { 27 fusion(string); 28 } 29} 30privatestaticvoid fusion(String file) { 31 File f=new File(file); 32 FileInputStream fis=null; 33 FileOutputStream fos=null; 34try { 35if(f.exists()){ 36if(f.isFile()){ 37 fis=new FileInputStream(f); 38//string="D:\\arch\\b.txtpart-0" 39 String srcPath=f.getAbsolutePath(); 40 String sep=""; 41if(srcPath.contains("\\")){ 42 sep="\\"; 43 } 44int index=srcPath.lastIndexOf(sep); 45 46//String s=srcPath.substring(0, (srcPath.length()-6)); 47//获取最后一个名字的字符串 48 String s=srcPath.substring(0, index+f.getName().length()); 49 fos=new FileOutputStream(new File(s),true); 50byte[] buf=newbyte[1024]; 51int len=0; 52while((len=fis.read(buf))!=-1){ 53 fos.write(buf, 0, len); 54 fos.close(); 55 } 56 } 57 } 58 fis.close(); 59 } catch (Exception e) { 60// TODO: handle exception 61 } 62} 63/** 64 * 文件的切割 65*/ 66publicstaticvoid split(String srcfile) { 67 68 File f = new File(srcfile); 69 FileOutputStream fos = null; 70try { 71 FileInputStream fis = new FileInputStream(f); 72// 获取文件大小 73int len = (int) f.length(); 74// 切割块数 75int blocks = 3; 76// 每块的大小 77int blocksSize = len / blocks; 78int len0 = 0; 79byte[] buf = newbyte[1024]; 80for (int i = 0; i < blocks; i++) { 81//根据源目录创建目标文件对象 82 fos = new FileOutputStream(new File(f.getParent(), f.getName() + "part-" + i)); 83// 如果是最后一块 84if (i == (blocks - 1)) { 85while ((len0 = fis.read(buf)) != -1) { 86 fos.write(buf, 0, len0); 87 } 88 fos.close(); 89 } 90// 不是最后一块 91else { 92// 不足缓冲区 93if (blocksSize <=buf.length) { 94 buf = newbyte[blocksSize]; 95 fis.read(buf); 96 fos.write(buf); 97 fos.close(); 98 } 99// 超过缓冲区100else { 101int count = blocksSize / buf.length; 102for (int j = 0; j < count; j++) { 103// 最后一次读取104if (j == (count - 1)) { 105if(blocksSize%buf.length !=0){ 106 buf = newbyte[buf.length + (blocksSize % buf.length)]; 107 fis.read(buf); 108 fos.write(buf); 109 fos.close(); 110 } 111 } 112else { 113 fis.read(buf); 114 fos.write(buf); 115 } 116 } 117 } 118 } 119 } 120 fis.close(); 121 } catch (Exception e) { 122// TODO Auto-generated catch block123 e.printStackTrace(); 124 } 125 } 126 }
原文:http://www.cnblogs.com/yihaifutai/p/6752031.html
内容总结
以上是互联网集市为您收集整理的javase文件切割及融合全部内容,希望文章能够帮你解决javase文件切割及融合所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。