-2
A
回答
2
爲了能夠將您的對象寫入(串行化)到流中,您的類應該實現Serializable接口。在大多數情況下,你不需要做任何事情,除了加入的類定義中「實現Serializable」條款:
class Person implements Serializable {
// your class's fields and methods
}
然後,當然,你是不是寫輸入流,而是一個輸出流:
Person p = new Person();
// some more code here...
OutputStream os = new FileOutputStream("persons.txt"); // open file as a stream
os.write(person); // write person object to the stream
os.close(); // close the stream
要轉換爲字節數組,你將不得不仍然使用序列:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(person);
byte[] bytes = baos.toByteArray();
相關問題
- 1. 對象轉換數組的InputStream
- 2. 追加InputStream對象爲String對象
- 3. 將InputStream讀入數據對象
- 4. 對象數組中的對象數組
- 5. 如何在同一個InputStream中讀取不同的數據組,對每個InputStream使用不同類型的InputStream?
- 6. 對象數組中的函數對象
- 7. 數組上的組對象?
- 8. 對象對象數組對象
- 9. 的CoffeeScript:對象的數組到對象
- 10. 對象 - 對象中的對象 - 這些對象的數組
- 11. 數組對象
- 12. 對象數組
- 13. 數組對象
- 14. 數組對象
- 15. 對象數組
- 16. 數組對象?
- 17. 對象數組
- 18. 對象數組
- 19. URL連接InputStream和OutputStream對象
- 20. java從InputStream創建InputStreamReader對象
- 21. InputStream對象轉換 - 彈簧集成
- 22. ByteArray或InputStream到文件對象
- 23. 從OMElement對象獲取InputStream/io.Reader
- 24. 從InputStream對象讀取文件
- 25. 如何將對象轉換爲InputStream
- 26. System.in如何在inputstream對象中?
- 27. Java I/O - 重用InputStream對象
- 28. 觀察對象的數組的數組的可觀察對象
- 29. mongodb向對象數組的對象數組插入參數
- 30. 數組對象數組
[序列化解釋](http://java.sun.com/developer/technicalArticles/Programming/serialization) – UmNyobe 2012-04-23 09:08:39
感謝您的回覆...基本上我用Person類作爲例子,但是我有一個帶有jar的類,它不是可序列化的。在這種情況下,我不能繼續進行序列化。 – Ritesh 2012-04-23 09:46:32
如果你不能使用序列化,那麼這個問題應該改寫。是的,它之前已經問過:http://stackoverflow.com/questions/239280/which-is-the-best-alternative-for-java-serialization – mindas 2012-04-23 09:53:48