从/到文件加载和保存LinkedLists

时间:2015-04-26 18:56:27

标签: java object linked-list

我正在研究库存控制程序。我有一个可用的每个项目的StockItem对象,以及一个StockLinkedList来存储多个不同的StockItem对象。我想知道是否有办法将我当前的LinkedList保存到文件,并使用对象流再次加载它。我尝试使用FileOutputStream和ObjectOutputStream,但无济于事。任何帮助将不胜感激。

目前我的StockLinkedList类的准系统如下:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.*;

public class StockLinkedList implements StockList {

    LinkedList<StockItem> stockItemList;

    public StockLinkedList(){
        stockItemList = new LinkedList<>();
    }

    public void loadStockData(String filename){
        try {
            FileOutputStream stockFile = new FileOutputStream(filename + ".dat");
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
        }
    }

    public void saveStockData(){
        ObjectOutputStream save = ObjectOutputStream(saveFile);
        save.writeObject(objectToSave);
        save.close();
    }

}

2 个答案:

答案 0 :(得分:3)

我建议你选择Json序列化。使用Gson库使用的启动示例。

    public static List<Weight> importDataFromFile(String path) {
        Gson g = new Gson();
        String s = FileHelper.readTextFromFile(path); (just read text from file)
        Type listType = new TypeToken<ArrayList<Weight>>() {
        }.getType();
        return g.fromJson(s, listType);
    }

    public static int exportDataToFile(String path, List<Weight> weights) {
       Gson g = new Gson();
       FileHelper.writeTextToFile(path, g.toJson(weights));
    }

我的样本中的重量对象是简单的POJO。 e.g:

public class Weight {
    private long date;
    private int value;
    private String unit;
    //geters setters
}

答案 1 :(得分:1)

如果您只想将对象的状态保存在文件中(序列化为文件),我建议您实现以下方法:

/**
 * This method will write your stock information into a file
 * @param path Is the target path in your file system
 * @param list Is the list you want to save
 * @throws IOException 
 * @throws FileNotFoundException 
 */
public void writeStockInfo(String path, LinkedList<StockItem> list) throws FileNotFoundException, IOException {
    //1. Point to your file using File
    File file =  new File(path);
    //2. Then use OOS but to serialize into a file you should use FileOutputStream inside the invocation
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
    //3. Just write the object to a file using the method "writeObject"
    objectOutputStream.writeObject(list);
    //4. Close the streams
    objectOutputStream.close();
}

/**
 * This method will read the information of the stock in a file
 * @param path Is the file from which you will read the info
 * @return
 * @throws IOException 
 * @throws FileNotFoundException 
 * @throws ClassNotFoundException 
 */
public LinkedList<StockItem> readStockInfo(String path) throws FileNotFoundException, IOException, ClassNotFoundException {
    LinkedList<StockItem> infoList = new LinkedList<StockItem>();
    //1. Point to your file (the one you want to read)
    File fileToRead = new File(path);
    //2. Use OIS to read the information from a file using FileInputStream
    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(fileToRead));
    //3. Just read the object and cast to make sure you obtain the right object
    infoList = (LinkedList<StockItem>) objectInputStream.readObject();
    //4. Close the stream
    objectInputStream.close();
    return infoList;
}

确保您的“StockItem”类正在实现Serializable接口。

顺便说一句:我编辑它是因为我忘了关闭溪流(对我感到羞耻!)

希望这会对你有所帮助。

快乐编码:)