【问题标题】:Problem serializing and deserializing ArrayListArrayList 序列化和反序列化问题
【发布时间】:2011-04-05 01:46:01
【问题描述】:

每当我尝试序列化文件时,我都会收到错误:FileNotFound。不知道为什么。这是我的 FileHelper 代码:

package org.stocktwits.helper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import org.stocktwits.model.Quote;

public class FileHelper {
    // Returns the contents of the file in a byte array.
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

    public static void serializeQuotes(ArrayList<Quote> quotes){
        try {
            // Serialize to a file
            ObjectOutput out = new ObjectOutputStream(new FileOutputStream("quotes.ser"));
            out.writeObject(quotes);
            out.close();

            // Serialize to a byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
            out = new ObjectOutputStream(bos) ;
            out.writeObject(quotes);
            out.close();

            // Get the bytes of the serialized object
            //byte[] buf = bos.toByteArray();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static void deserializeQuotes(ArrayList<Quote> quotes){
        try {
            // Deserialize from a file
            File file = new File("quotes.ser");
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            // Deserialize the object
            quotes = (ArrayList<Quote>) in.readObject();
            in.close();

            // Get some byte array data
            byte[] bytes = FileHelper.getBytesFromFile(file);
            // see Reading a File into a Byte Array for the implementation of this method

            // Deserialize from a byte array
            in = new ObjectInputStream(new ByteArrayInputStream(bytes));
            quotes = (ArrayList<Quote>) in.readObject();
            in.close();
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
    }   
}

【问题讨论】:

  • 啊,我记得以前做过这样的事情。最后我们只是放弃并实现了我们自己的(反)序列化方法。
  • 可以包含堆栈跟踪吗?
  • 请将System.out.println(e); 替换为e.printStackTrace() 或只是throw e
  • @NullUser:你写了自己的序列化,因为你无法打开文件?
  • @EJP 否,因为无法正确反序列化对象。

标签: java android serialization arraylist


【解决方案1】:
private void serializeQuotes(){
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void deserializeQuotes(){
        try{
            FileInputStream fis = openFileInput(Constants.FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            quotes = (ArrayList<Quote>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

【讨论】:

  • 实际上你通过修复 FileNotFoundException 的原因解决了它,它与这段代码没有任何关系,虽然它是巨大的改进。
  • 是的,Context.MODE_PRIVATE 解决了 Android 的 FileNotFoundException