文件合并时关于Enumeration使用例子

-------android培训JAVA培训、期待与您交流! ----------

在JAVA学习过程中,关于文件的合并,代码对于枚举这块不是很明白,特地搜寻查找了一下资料学习

问题代码如下:

public void sequence_demo() throws IOException{//合并
                FileOutputStream op = new FileOutputStream("Sequence.zip");//定义合并后的目的地
                
                FileInputStream fs = null;
                
                //合并,需要使用类SequenceInputStream,经过查询API得知构造方法
                //SequenceInputStream(Enumeration<? extends InputStream>e),参数是枚举类型,泛型是InputStream
                //由于Enumeration枚举属于集合类,属于vactor类中,但是vactor效率不高,那么可以将ArrayList转换一下则:
                ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
                for(int i=1;i<=5;i++)//正常不应该写死,本测试知道已经将文件切割成5个人文件,所以此处为5
                {
                        al.add(new FileInputStream(i+".part"));
                }
               
                final Iterator<FileInputStream> it = al.iterator();
                Enumeration<FileInputStream> et = new Enumeration<FileInputStream>(){
                        public boolean hasMoreElements(){
                                return it.hasNext();
                        }
                        public FileInputStream nextElement(){
                                return it.next();
                        }
                };
                SequenceInputStream sis = new SequenceInputStream(et);
              
                byte[] bt = new byte[1024];
                int len = 0;
                while((len = sis.read(bt))!=-1){
                        op.write(bt, 0, len);
                }
                op.close();
                sis.close();
                

        }


学习关于Enumeration接口结果如下:

Enumeration接口本身不是一个数据结构。但是,对其他数据结构非常重要。 Enumeration接口定义了从一个数据结构得到连续数据的手段。例如,Enumeration定义了一个名为nextElement的方法,可以用来从含有多个元素的数据结构中得到的下一个元素。 
Enumeration接口提供了一套标准的方法,由于Enumeration是一个接口,它的角色局限于为数据结构提供方法协议。下面是一个使用的例子: 
//e is an object that implements the Enumeration interface
while (e.hasMoreElements()) {
    Object o= e.nextElement();
    System.out.println(o);

实现该接口的对象由一系列的元素组成,可以连续地调用nextElement()方法来得到 Enumeration枚举对象中的元素。Enumertion接口中仅定义了下面两个方法。 
·boolean hasMoreElemerts() 
测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。 
·Object nextElement() 
如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。
【例】
/*
* @(#)DemoEnumeration.java 
* 演示Enumeration接口的使用
* / 
import java.util.*;
class DemoEnumeration{ 
     public static void main(String[] args){
          //实例化MyDataStruct类型的对象
          MyDataStruct mySataStruct=new myDataStruct();
          //得到描述myDataStruct类型对象的enumeration对象
          Enumeration myEnumeration =myDataStruct.getEnum();
         //使用对象循环显示myDataStruct类型的对象中的每一个元素
         while (myEnumeration.hasMoreElements())
               System.out.println(myEnumeration.nextElement());
    } 
}
//MyEnumeration类实现Enumeration接口
class MyEnumerator implements Enumeration

      int count; // 计数器
      int length; //存储的数组的长度
      object[] dataArray; // 存储数据数组的引用
      //构造器
      MyEnumeration(int count,int length,object[] dataArray){ 
            this.count = count;
            this.length= length;
            this.dataArray=dataArray;
      } 
      public boolean hasMoreElements() { 
            return (count< length);
      }
      public Object nextElement() {
            return dataArray[count++];
      }

//MyDataStruct类用于实例化一个简单的、可以提供enumeration对象
//给使用程序的数据结果对象
class MyDataSttuct

     String[] data;
     // 构造器
     MyDataStruct(){
          data=new String[4] 
          data[0] ="zero";
          data[1]="one";
          data[2] ="two";
          data[3]="three";
    }
    // 返回一个enumeration对象给使用程序
    Enumeration getEnum() {
          return new MyEnumeration(0,data.length,data);
    }
程序的运行结果为: 
zero
one
two
three


                -------android培训JAVA培训、期待与您交流! ----------

--------详细请查看www.itheima.com-------------


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章