定义byte[] buffer时指定为file.getSize()的长度

解决方案 »

  1.   

    // Create an object and set properties
        MyClass o = new MyClass();
        o.setProp(1);
        o.setProps(new int[]{1, 2, 3});
        
        try {
            // Serialize object into XML
            XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                new FileOutputStream("outfilename.xml")));
            encoder.writeObject(o);
            encoder.close();
        } catch (FileNotFoundException e) {
        }    // This class defines two properties - prop and props
        public class MyClass {
            // The prop property
            int i;
            public int getProp() {
                return i;
            }
            public void setProp(int i) {
                this.i = i;
            }
        
            // The props property
            int[] iarray = new int[0];
            public int[] getProps() {
                return iarray;
            }
            public void setProps(int[] iarray) {
                this.iarray = iarray;
            }
        }
      

  2.   

    可以到http://javaalmanac.com看看例子