加载和保存 LinkedLists from/to 文件
Loading and Saving LinkedLists from/to file
我正在研究库存控制程序。我有一个用于每个可用项目的 StockItem 对象,以及一个用于存储多个不同 StockItem 对象的 StockLinkedList。我想知道是否有办法将我当前的 LinkedList 保存到文件,并使用对象流再次加载它。我试图尝试使用 FileOutputStream 和 ObjectOutputStream 但无济于事。任何帮助将不胜感激。
目前我的 StockLinkedList class 的准系统如下:
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 序列化。使用 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
}
如果您只想将对象的状态保存在文件中(序列化为文件),我建议您实现以下方法:
/**
* 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" class 正在实现 Serializable 接口。
顺便说一句:我编辑它是因为我忘记关闭流了(真丢人!)
希望对您有所帮助。
编码愉快:)
我正在研究库存控制程序。我有一个用于每个可用项目的 StockItem 对象,以及一个用于存储多个不同 StockItem 对象的 StockLinkedList。我想知道是否有办法将我当前的 LinkedList 保存到文件,并使用对象流再次加载它。我试图尝试使用 FileOutputStream 和 ObjectOutputStream 但无济于事。任何帮助将不胜感激。
目前我的 StockLinkedList class 的准系统如下:
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 序列化。使用 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
}
如果您只想将对象的状态保存在文件中(序列化为文件),我建议您实现以下方法:
/**
* 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" class 正在实现 Serializable 接口。
顺便说一句:我编辑它是因为我忘记关闭流了(真丢人!)
希望对您有所帮助。
编码愉快:)