Java 如何對檔案進行多個Object物件流程的讀寫操作

來源:互聯網
上載者:User

思路:把已經序列化的對象存入容器(如LinkedList<?>)中,然後用ObjectInputStream和ObjectOutputStream對這個執行個體化的LinkedList<?>對象進行讀寫。

測試主程式:


/**   * @Title: FileRW.java* @Package com.file* @Description: 檔案、檔案夾的建立、寫入練習。讀寫是使用物件流程實現。* @author 慢跑學Android* @date 2011-11-19 下午03:53:01* @version V1.0   */package com.file;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.LinkedList;public class FileRW {private String dirPath;private String filename;public static void main(String[] args) {String path = "C:\\曉聲";String fileName = "test.txt";FileRW fileRW = new FileRW(path, fileName);LinkedList<TestMessage> msgOut = new LinkedList<TestMessage>();LinkedList<TestMessage> msgIn = null;msgOut.add(new TestMessage("柯南", "偶像"));msgOut.add(new TestMessage("卡卡西", "好樣的"));msgOut.add(new TestMessage("Android", "Android"));msgOut.add(new TestMessage("哈哈", "測試下喔"));fileRW.writeObject(path, fileName, msgOut);msgIn = fileRW.readObject(path,fileName);for(TestMessage temp:msgIn){System.out.println(temp.getName() + temp.getData());}}public FileRW(String dirPath, String filename) {this.dirPath = dirPath;this.filename = filename;if (creatDir()) {creatFile();}}private boolean creatDir() {if (null != dirPath) {File path = new File(dirPath);if (path.exists()) {return true;}if (true == path.mkdirs() ) {return true;}}return false;}private void creatFile() {if (null != filename) {File file = new File(dirPath, filename);if (false == file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}}}/*** @Title: writeObject* @Description: Write a object to a file.* @param path the directory of the target file* @param filename the name of the target file* @param msg the type of the object* @return void* @throws*/private void writeObject(String path, String filename, LinkedList<TestMessage> msg) {File file = new File(path, filename);if (false == file.isFile()) {return ;}try {// The value "false" for FileOutputStream means that overwrite this file,// if it is "true",append the new data to this file.ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,false));oos.writeObject(msg);oos.flush();oos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/*** @Title: readObject* @Description: Read a object from a file.* @param path the directory of the target file* @param filename the name of the target file* @return LinkedList<TestMessage>* @throws*/@SuppressWarnings("unchecked")private LinkedList<TestMessage> readObject(String path, String filename) {File file = new File(path, filename);ObjectInputStream ois = null;LinkedList<TestMessage> msgAll = null;try {ois = new ObjectInputStream(new FileInputStream(file));try {msgAll = (LinkedList<TestMessage>)ois.readObject();} catch (ClassNotFoundException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {ois.close();} catch (IOException e) {e.printStackTrace();}}return msgAll;}}

測試程式中的訊息包定義:

/**   * @Title: TestMessage.java* @Package com.file* @Description: FileRW的訊息流程* @author 慢跑學Android* @date 2011-11-19 下午04:35:11* @version V1.0   */package com.file;public class TestMessage implements java.io.Serializable {private String name;private String data;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getData() {return data;}public void setData(String data) {this.data = data;}public TestMessage(String name, String msg) {this.name = name;data = msg;}}

程式運行結果:

參考資料:ObjectInputStream類和ObjectOutputStream類的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.