【问题标题】:Loading and Saving LinkedLists from/to file从文件加载和保存链接列表
【发布时间】:2015-04-26 18:56:27
【问题描述】:

我正在制定一个库存控制计划。对于每个可用的项目,我都有一个 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();
    }

}

【问题讨论】:

  • 将其序列化为 JSON 并返回不符合您的需求?
  • 也许你误解了使用流的方式。当你要读取信息时,你应该使用InputStream,而要写入,你应该使用OutputStream。
  • @MarceloTataje 是的,我也是。我正在通过谷歌学习所有这些,发现有些人正在使用 FileOutputStream 加载文件,这让我很困惑,但我还是尝试了一下
  • @Divers JSON 不只是 JS 吗?这是一个标准的 Java 控制台应用程序
  • 您需要什么格式来显示您的信息?文本文件,JSON,XML,序列化成文件?

标签: java object linked-list


【解决方案1】:

我建议你使用 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。例如:

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

【讨论】:

    【解决方案2】:

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

    /**
     * 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 接口。

    顺便说一句:我编辑它是因为我忘记关闭流(真丢脸!)

    希望对你有所帮助。

    编码愉快:)

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多