案例:对本地一张微信保存的图片进行分割和合并
package com.lx21; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class FileDevisionMergeDemo { private static void division(File targetFile ,long cutsize) { if (targetFile==null) return; int num = targetFile.length()%cutsize==0? (int)(targetFile.length()/cutsize):(int)(targetFile.length()/cutsize+1); //构造输入流 try { BufferedInputStream in = new BufferedInputStream(new FileInputStream(targetFile)); BufferedOutputStream out = null; byte[] bytes = null; int len = -1; int count = 0;//文件读取的次数 for(int i = 0;i0&&(len=in.read(bytes))!=-1){ out.write(bytes,0,len); out.flush(); count--; } if(cutsize%1024!=0) { bytes = new byte[(int)cutsize%1024]; len = in.read(bytes); out.write(bytes, 0, len); out.flush(); out.close(); } } in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unused") private static void merge(Enumeration es) { //构造合并流 SequenceInputStream sis = new SequenceInputStream(es); try { BufferedOutputStream bos =new BufferedOutputStream(new FileOutputStream("D:\javatest\wechat.png")); byte[] bytes = new byte[1024]; int len = -1; while((len=sis.read(bytes))!=-1) { bos.write(bytes,0,len); bos.flush(); } bos.close(); sis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { File file = new File ("D:\javatest\20220102131730.jpg") ; division(file, 1024*1024); try { InputStream in1 =new FileInputStream(new File("D:\javatest1-temp-20220102131730.jpg")); InputStream in2 =new FileInputStream(new File("D:\javatest\javatest2-temp-20220102131730.jpg")); InputStream in3 =new FileInputStream(new File("D:\javatest\javatest3-temp-20220102131730.jpg")); InputStream in4 =new FileInputStream(new File("D:\javatest\javatest4-temp-20220102131730.jpg")); InputStream in5 =new FileInputStream(new File("D:\javatest\javatest5-temp-20220102131730.jpg")); InputStream in6 =new FileInputStream(new File("D:\javatest\javatest6-temp-20220102131730.jpg")); InputStream in7 =new FileInputStream(new File("D:\javatest\javatest7-temp-20220102131730.jpg")); InputStream in8 =new FileInputStream(new File("D:\javatest\javatest8-temp-20220102131730.jpg")); InputStream in9 =new FileInputStream(new File("D:\javatest\javatest9-temp-20220102131730.jpg")); InputStream in10 =new FileInputStream(new File("D:\javatest\javatest10-temp-20220102131730.jpg")); InputStream in11 =new FileInputStream(new File("D:\javatest\javatest11-temp-20220102131730.jpg")); InputStream in12 =new FileInputStream(new File("D:\javatest\javatest12-temp-20220102131730.jpg")); InputStream in13 =new FileInputStream(new File("D:\javatest\javatest13-temp-20220102131730.jpg")); InputStream in14 =new FileInputStream(new File("D:\javatest\javatest14-temp-20220102131730.jpg")); InputStream in15 =new FileInputStream(new File("D:\javatest\javatest15-temp-20220102131730.jpg")); Vector e = new Vector (); e.add(in1); e.add(in2); e.add(in3); e.add(in4); e.add(in5); e.add(in6); e.add(in7); e.add(in8); e.add(in9); e.add(in10); e.add(in11); e.add(in12); e.add(in13); e.add(in14); e.add(in15); Enumeration es = e.elements(); merge(es); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)