使用多个对象反序列化ArrayList

时间:2016-05-03 14:22:27

标签: java arraylist deserialization

我在教师ArrayList中存储了多个对象,我正在尝试对其进行反序列化,但它显示了垃圾值。

public void Write(){
    try{
        FileOutputStream fs=new FileOutputStream("S.txt");
        ObjectOutputStream os=new ObjectOutputStream(fs);
        os.writeObject(teachers);

        os.close();
    }catch(Exception e)
    {System.out.println(e);}

}


public void Read(){
    try
    {
        FileInputStream fis = new FileInputStream("E:\\Books\\OOP\\Teacher\\S.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        teachers = (ArrayList<Teacher>) ois.readObject();
        for(Teacher i: teachers){
            System.out.println(i.toString());
        }
        ois.close();
        fis.close();
    }catch(IOException ioe){
        System.out.println("Error");
        return;
    }catch(ClassNotFoundException c){
        System.out.println("Class not found");

        return;
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用对象流,它将您的序列化对象保存为二进制文件,而不管文件的扩展名(这里恰好是“.txt”,模糊地表明它在编写时将是一个文本文件)。

当写入的数据是二进制数据时,您无法使用文本编辑器从文件中查看任何类似人的内容。

如果在反序列化时迭代List<Teacher>时也会打印出“垃圾值”,则表示:

  1. (De-)序列化实际上已成功(即您已将List<Teacher>写入文件并将其检索出来)
  2. 您的Teacher类可能无法正确覆盖toString或根本不会覆盖Type@hashCode,因此在将Object#toString表示法输入系统输出打印流时会从Process: Mix-ide [29364] Path: /usr/local/Cellar/cpp-ethereum/1.2.4/Mix-ide.app/Contents/MacOS/Mix-ide Identifier: . Version: ??? (mix ) Code Type: X86-64 (Native) Parent Process: ??? [1] Responsible: Mix-ide [29364] User ID: 501 Date/Time: 2016-05-03 16:09:56.394 +0200 OS Version: Mac OS X 10.11.4 (15E65) Report Version: 11 Anonymous UUID: 13C99257-E029-CFC4-7857-A40E5D5F192E Sleep/Wake UUID: 56AA9A03-8627-4D70-87F0-8D3C65C820CA Time Awake Since Boot: 97000 seconds Time Since Wake: 5700 seconds System Integrity Protection: enabled Crashed Thread: 3 QThread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: EXC_I386_GPFLT Exception Note: EXC_CORPSE_NOTIFY Thread 0:: CrBrowserMain Dispatch queue: com.apple.main-thread 0 libethereum.dylib 0x0000000113024880 0x112f4b000 + 891008 1 libethereum.dylib 0x0000000112fdeb9d dev::eth::ChainParams::genesisBlock() const + 1081 2 libethereum.dylib 0x0000000112f9264c dev::eth::BlockChain::genesis() const + 164 3 libethereum.dylib 0x0000000112f921db dev::eth::BlockChain::init(dev::eth::ChainParams const&, std::__1::basic_string, std::__1::allocator > const&) + 171 4 libethereum.dylib 0x0000000112f91e9f dev::eth::BlockChain::BlockChain(dev::eth::ChainParams const&, std::__1::basic_string, std::__1::allocator > const&, dev::WithExisting, std::__1::function const&) + 979 5 . 0x000000010d03afbd dev::mix::MixBlockChain::MixBlockChain(std::__1::basic_string, std::__1::allocator > const&, std::__1::unordered_mapdev::FixedHash<20u, dev::eth::Account, std::__1::hashdev::FixedHash<20u >, std::__1::equal_todev::FixedHash<20u >, std::__1::allocatorstd::__1::pair<dev::FixedHash<20u const, dev::eth::Account> > > const&) + 99 6 . 0x000000010d03b864 dev::mix::MixClient::resetState(std::__1::unordered_mapdev::FixedHash<20u, dev::eth::Account, std::__1::hashdev::FixedHash<20u >, std::__1::equal_todev::FixedHash<20u >, std::__1::allocatorstd::__1::pair<dev::FixedHash<20u const, dev::eth::Account> > > const&, dev::SecureFixedHash<32u> const&) + 490 7 . 0x000000010d03b51c dev::mix::MixClient::MixClient(std::__1::basic_string, std::__1::allocator > const&) + 300 8 . 0x000000010cfd96da dev::mix::ClientModel::init(QString) + 446 9 . 0x000000010d073cc8 0x10cfcc000 + 687304 10 . 0x000000010d074aea dev::mix::ClientModel::qt_metacall(QMetaObject::Call, int, void) + 112 11 org.qt-project.QtQml 0x00000001123c540b 0x112248000 + 1561611 12 org.qt-project.QtQml 0x00000001123c3ec0 0x112248000 + 1556160 13 org.qt-project.QtQml 0x00000001123c326b QV4::QObjectMethod::callInternal(QV4::CallData) const + 1099 14 org.qt-project.QtQml 0x00000001123d8656 QV4::Runtime::callProperty(QV4::ExecutionEngine, int, QV4::CallData) + 1206 15 ??? 0x0000000118c5af7e 0 + 4710575998 16 org.qt-project.QtQml 0x000000011236fb59 0x112248000 + 1211225 17 org.qt-project.QtQml 0x0000000112467894 QQmlJavaScriptExpression::evaluate(QV4::CallData, bool*) + 644 18 org.qt-project.QtQml 0x000000011240e26d QQmlBoundSignalExpression::evaluate(void) + 829 19 org.qt-project.QtQml 0x000000011240e975 0x112248000 + 1862005 20 org.qt-project.QtQml 0x00000001124482f5 QQmlNotifier::emitNotify(QQmlNotifierEndpoint, void) + 741 21 org.qt-project.QtCore 0x00000001129891a7 QMetaObject::activate(QObject, int, int, void) + 199 22 org.qt-project.QtQml 0x000000011247d5fd 0x112248000 + 2315773 23 org.qt-project.QtQml 0x0000000112404049 QQmlComponentPrivate::complete(QQmlEnginePrivate, QQmlComponentPrivate::ConstructionState) + 73 24 org.qt-project.QtQml 0x0000000112402269 QQmlComponentPrivate::completeCreate() + 41 25 org.qt-project.QtQml 0x0000000112403a22 QQmlComponent::create(QQmlContext) + 114 26 org.qt-project.QtQml 0x000000011246fcbf QQmlApplicationEnginePrivate::_q_finishLoad(QObject) + 111 27 org.qt-project.QtQml 0x0000000112470292 QQmlApplicationEngine::load(QUrl const&) + 34 28 . 0x000000010d033cb1 dev::mix::MixApplication::MixApplication(int&, char) + 115 29 . 0x000000010d033789 main + 42 30 libdyld.dylib 0x00007fff91fe15ad start + 1 Thread 1:: Dispatch queue: com.apple.libdispatch-manager 0 libsystem_kernel.dylib 0x00007fff97dfdefa kevent_qos + 10 1 libdispatch.dylib 0x00007fff930df165 _dispatch_mgr_invoke + 216 2 libdispatch.dylib 0x00007fff930dedcd _dispatch_mgr_thread + 52 Thread 2:: QQmlThread 0 libsystem_kernel.dylib 0x00007fff97dfd07a __select + 10 1 org.qt-project.QtCore 0x00000001129aaf43 qt_safe_select(int, fd_set, fd_set, fd_set, timespec const) + 547 2 org.qt-project.QtCore 0x00000001129abe75 QEventDispatcherUNIXPrivate::doSelect(QFlagsQEventLoop::ProcessEventsFlag, timespec*) + 709 3 org.qt-project.QtCore 0x00000001129ad2b7 QEventDispatcherUNIX::processEvents(QFlagsQEventLoop::ProcessEventsFlag) + 231 4 org.qt-project.QtCore 0x000000011294ffe5 QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) + 421 5 org.qt-project.QtCore 0x000000011278a843 QThread::exec() + 115 6 org.qt-project.QtQml 0x0000000112481e89 0x112248000 + 2334345 7 org.qt-project.QtCore 0x000000011278e2fa 0x112765000 + 168698 8 libsystem_pthread.dylib 0x00007fff9979199d _pthread_body + 131 9 libsystem_pthread.dylib 0x00007fff9979191a _pthread_start + 168 10 libsystem_pthread.dylib 0x00007fff9978f351 thread_start + 13 打印
相关问题