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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王月 中级黑马   /  2012-6-12 21:58  /  2766 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王月 于 2012-6-12 22:03 编辑

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Temp {

public static void main(String[] args) throws IOException {
  /*
   * 需求:切割图片
   */
  // 1,创建流对象的引用。
  FileInputStream fr = new FileInputStream("hh.jpg");
  FileOutputStream fw1 = new FileOutputStream("hh1.jpg");
  FileOutputStream fw2 = new FileOutputStream("hh2.jpg");
  FileOutputStream fw3 = new FileOutputStream("hh3.jpg");
   
   // 创建一个字符数组作为缓冲区。
   byte[] buf = new byte[50000];
   // 进行读写操作。
  
   int count =0;
   int ch= 0;
   while ((ch = fr.read(buf)) != -1) {
    if(count==0)
    {
    fw1.write(buf);
    count++;
    fw1.flush();
    }
   
    if((count==1)&&(ch = fr.read(buf)) != -1){
     fw2.write(buf);
     count++;
     fw2.flush();
    }
    if((count==2)&&(ch = fr.read(buf)) != -1)
    {
     fw3.write(buf);
     fw3.flush();
      break;//因为我那张图片只有100kb左右,所以想把它只切成三份。
    }
   }
   fw1.close();
   fw2.close();
   fw3.close();
  }
  
}

我也看了毕老师的视频,可我感觉这样也可以呀,只是我自己将切割的图片分别写入了三个图片文件中。可为什么不行呢。
请教各位高手了

4 个回复

倒序浏览
首先 你的 if((count==1)&&(ch = fr.read(buf)) != -1) 这地方 你的(这时英语的)写成了(这是中文的)
还有你的
temp.java:39:
      break;//这地方 你也写成中文的了


仔细一点{:soso_e112:}
回复 使用道具 举报
1.你判断if语句中count值得语句使用了中文括号,应改为英文的。
2.break后面的分号也应该是英文形式的。
3. 输出语句应该输出的内容时读入到数组中的有效内容,例如:fw1.write(buf); ---------------->fw1.write(buf,0,ch);

回复 使用道具 举报
注意你的中英文,我帮你改过了 正确的可执行代码如下。
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class Temp {

  6. public static void main(String[] args) throws IOException {
  7.   /*
  8.    * 需求:切割图片
  9.    */
  10.   // 1,创建流对象的引用。
  11.   FileInputStream fr = new FileInputStream("C:\\1.1.jpg");
  12.   FileOutputStream fw1 = new FileOutputStream("d:\\hh1.jpg");
  13.   FileOutputStream fw2 = new FileOutputStream("d:\\hh2.jpg");
  14.   FileOutputStream fw3 = new FileOutputStream("d:\\hh3.jpg");
  15.    
  16.    // 创建一个字符数组作为缓冲区。
  17.    byte[] buf = new byte[50000];
  18.    // 进行读写操作。
  19.   
  20.    int count =0;
  21.    int ch= 0;
  22.    while ((ch = fr.read(buf)) != -1) {
  23.     if(count==0)
  24.     {
  25.     fw1.write(buf);
  26.     count++;
  27.     fw1.flush();
  28.     }
  29.    
  30.     if((count==1)&&(ch = fr.read(buf)) != -1){
  31.      fw2.write(buf);
  32.      count++;
  33.      fw2.flush();
  34.     }
  35.     if((count==2)&&(ch = fr.read(buf)) != -1)
  36.     {
  37.      fw3.write(buf);
  38.      fw3.flush();
  39.       break;//因为我那张图片只有100kb左右,所以想把它只切成三份。
  40.     }
  41.    }
  42.    fw1.close();
  43.    fw2.close();
  44.    fw3.close();
  45.   }
  46.   
  47. }
复制代码
回复 使用道具 举报
用这个把,肯定行,

import java.io.*;
import java.util.*;

class Spiltfiledemo
{
        public static void main(String[] args) throws IOException
        {
                //spiltfile();
                merge();
        }
        //用合并流合并数据
        public static void merge() throws IOException
        {
                ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
                for (int x=1;x<=3 ;x++ )
                {
                        al.add(new FileInputStream("d:\\spilt\\"+x+".part"));//将所有切割的碎片添加到数组中
                }
                final Iterator<FileInputStream> it=al.iterator();
                Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
                {
                                public boolean hasMoreElements()
                            {
                                        return it.hasNext();//判断是否还有下一个元素                               
                                }
                                public FileInputStream nextElement()
                            {                               
                                return it.next();//如歌还有这放回下一个元素
                                }
                };
                SequenceInputStream sis=new SequenceInputStream(en);//读取所有集合中的文件,
                FileOutputStream fos=new FileOutputStream("d:\\spilt\\1.bmp");//目的文件
                byte[] b=new byte[1024*10];
                int len=0;
                while ((len=sis.read(b))!=-1)
                {
                        fos.write(b,0,len);                       
                }
                fos.close();
                sis.close();
               
       
        }
        public static void spiltfile()throws IOException
        {
                FileInputStream fis=new FileInputStream("d:\\2.jpg");
                FileOutputStream fos=null;
                byte[] b=new byte[1024*300];//第一个字节缓冲区
                int len=0;
                int count=1;
                while ((len=fis.read(b))!=-1)//将数据读取到byte中
                {
                        fos=new FileOutputStream("d:\\spilt\\"+(count++)+".part");//将切割的碎片出入到spilt文件中       
                        fos.write(b,0,len);
                        fos.close();
                }
                fis.close();


       
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马