A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yninggis 中级黑马   /  2014-12-23 00:43  /  770 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天在做图片的切割与合并的练习,发现一个奇怪的问题:在将碎片合并过程中,用于存储数据的byte数组大小不同,会得出不同大小的图片,代码如下。先谢谢各位了!
public static void merge()throws IOException
        {
                Vector<FileInputStream> v = new Vector<FileInputStream>();
                for (int x=1;x<=4 ;x++ )
                {
                        v.add(new FileInputStream("e:\\javaTest\\"+x+".part"));
                }
                Enumeration<FileInputStream> en = v.elements();

                SequenceInputStream seq = new SequenceInputStream(en);
                FileOutputStream fos = new FileOutputStream("e:\\javaTest\\5.jpg");
               
                int len = 0;
                byte[] b = new byte[1024];//合并后的图片大小为3,445,760字节.
                //byte[] b = new byte[1024*1024];//合并后的图片大小为4,194,304字节。
                while ((len=seq.read(b))!=-1)
                {
                        fos.write(b);
                }
                fos.close();
                seq.close();
        }

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

9 个回复

倒序浏览
你那个fos.write(b) 把整个字节数组都写进去了,而不是写的字节数组有文字的部分,我估计,你要是把字节数组弄成了1024*1024然后你试着合并100个文件,每个文件就写一个字,你8成要重启,试试看看。试玩了告诉我结果
我写的合并代码你看看吧也许有点用也许没有
  1. public static void show1() throws IOException {  
  2.         Vector<InputStream> v = new Vector<InputStream>();  
  3.         v.add(new FileInputStream("1.txt"));  
  4.         v.add(new FileInputStream("2.txt"));  
  5.         v.add(new FileInputStream("3.txt"));  
  6.         Enumeration<InputStream> en = v.elements();//获得枚举变量  
  7.         SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
  8.          
  9.         FileOutputStream fos = new FileOutputStream(new File("4.txt"));  
  10.          
  11.         int len = 0;  
  12.         byte []bu = new byte[1024];  
  13.         while((len = sis.read(bu))!=-1){  
  14.             fos.write(bu, 0, len);  
  15.         }     
  16.         sis.close();  
  17.         fos.close();  
  18.     }  
  19. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
  1. public static void show1() throws IOException {  
  2.         Vector<InputStream> v = new Vector<InputStream>();  
  3.         v.add(new FileInputStream("1.txt"));  
  4.         v.add(new FileInputStream("2.txt"));  
  5.         v.add(new FileInputStream("3.txt"));  
  6.         Enumeration<InputStream> en = v.elements();//获得枚举变量  
  7.         SequenceInputStream sis = new SequenceInputStream(en);//将三个数据源合并到一个数据源  
  8.          
  9.         FileOutputStream fos = new FileOutputStream(new File("4.txt"));  
  10.          
  11.         int len = 0;  
  12.         byte []bu = new byte[1024];  
  13.         while((len = sis.read(bu))!=-1){  
  14.             fos.write(bu, 0, len);  
  15.         }     
  16.         sis.close();  
  17.         fos.close();  
  18.     }  
  19. }
复制代码
回复 使用道具 举报
fos.write(b, 0, len);  

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
这是我自己做的文件合并与切割,亲可以看一下。。。。。。。
//将指定文件分割成指定的份数
         private static void divide(String src ,int m) {
                 long start =System. currentTimeMillis();
                 //目标文件
                File file=new File (src);
                 if(file .isFile()){
                 //文件的长度
                 int l =(int )file. length();
                 //获取目标目标文件的名字
                String filename=file .getName() ;
                 //System.out.println(filename);
                 //每一份文件的长度
                 int L =l/ m;
                 //获取截取之后的文件的名字
                String parentName=file .getParent() ;
                String beforName=filename .substring( 0, filename.lastIndexOf( "."));
                String endName=filename .substring( filename.indexOf (".")) ;
                 //System.out.println(parentName);
         //     System.out.println(beforName);
         //     System.out.println(endName);
                 //创建流对象
                BufferedInputStream bi=null;
                BufferedOutputStream bo=null;
                 try {
                        bi =new BufferedInputStream( new FileInputStream(file ));
                         byte[] bytes=new byte [1024* 1024];
                         int len =-1;
                         for (int i = 1; i <=m; i++) {
                                String name=parentName +File. separator+beforName +"_"+ i+endName ;
                                File file2=new File (name);
                                bo =new BufferedOutputStream( new FileOutputStream(file2 ));
                                 //开始读写了
                                 while((len =bi. read(bytes ))!=-1 ){
                                        bo .write(bytes, 0, len );
                                         if (file2. length()> L) {
                                                 break;
                                         }
                                        bo .flush() ;
                                 }
                         }
                 }  catch (IOException e) {
                        e .printStackTrace() ;
                 }finally{
                         if (bo!= null) {
                                 try {
                                        bo .close() ;
                                 } catch (IOException e) {
                                        e .printStackTrace() ;
                                 }
                         }
                         if (bi!= null) {
                                 try {
                                        bi .close() ;
                                 } catch (IOException e) {
                                        e .printStackTrace() ;
                                 }
                         }
                         long end =System. currentTimeMillis();
                        System .out. println("分割时间为:" +(end -start)) ;
                        System .out. println("分割完成!" );
                 }
                 }
         }

//将指定份数的文件合并为一个文件
         private static void append(String ...name) {
                 //获取目标文件
                 int n =name. length;
                 //要写入的目标文件的名字
                String beforName=name [0].substring (0, name[0].indexOf ("_")) ;
                String endName=name [0].substring (name[0].indexOf (".")) ;
                StringBuffer fileName=new StringBuffer ();
                fileName .append( beforName). append(endName );
                File file=new File (fileName. toString());
                 //System.out.println(fileName);
                 //创建流对象
                BufferedInputStream bi=null;
                BufferedOutputStream bo=null;
                 try {
                        bo =new BufferedOutputStream( new FileOutputStream(file ,true ));//追加
                         for (int i = 0; i < name. length; i ++) {
                                 File file1=new File (name[i]);
                                 bi =new BufferedInputStream( new FileInputStream(file1 ));
                                 //开始读写
                                 byte[] bytes=new byte [1024* 1024];
                                 int len =-1;
                                 while((len =bi. read(bytes ))!=-1 ){
                                                bo. write(bytes , 0, len);
                                                bo. flush();
                                 }
                         }
                 }  catch (IOException e) {
                        e .printStackTrace() ;
                 }finally{
                         if (bo!= null) {
                                 try {
                                        bo .close() ;
                                 } catch (IOException e) {
                                        e .printStackTrace() ;
                                 }
                         }if (bi!= null) {
                                 try {
                                        bi .close() ;
                                 } catch (IOException e) {
                                        e .printStackTrace() ;
                                 }
                         }
                 }
                System .out. println("合并完成!" );
         }
回复 使用道具 举报
wangcongwu 发表于 2014-12-23 04:26
你那个fos.write(b) 把整个字节数组都写进去了,而不是写的字节数组有文字的部分,我估计,你要是把字节数 ...

我这样玩过,一个mp3 格式的文件 我分割在byte[1024]里面   结果分割了多少我是不清
楚因为就是没完,我看的一个数字是7百多万了,之后删除,删了半个小时……
回复 使用道具 举报
找寻小龙猫 发表于 2014-12-29 13:15
我这样玩过,一个mp3 格式的文件 我分割在byte[1024]里面   结果分割了多少我是不清
楚因为就是没完,我 ...

是用我的那个程序吗

点评

不是  发表于 2014-12-30 18:19
回复 使用道具 举报

好的,非常感谢!我知道怎么回事了,是我把整个数组都输出来了。你的代码还没来得及试,试过了给你答复。再次感谢!
回复 使用道具 举报
Eagle 发表于 2014-12-23 07:11
fos.write(b, 0, len);

好的,了解了,谢谢你!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马