Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将LinkedList从/保存到文件_Java_Object_Linked List - Fatal编程技术网

Java 将LinkedList从/保存到文件

Java 将LinkedList从/保存到文件,java,object,linked-list,Java,Object,Linked List,我在做一个库存控制计划。对于每个可用的项目,我都有一个StockItem对象,还有一个StockLinkedList来存储多个不同的StockItem对象。我想知道是否有办法将我当前的LinkedList保存到文件中,并使用对象流再次加载它。我尝试使用FileOutputStream和ObjectOutputStream,但没有效果。任何帮助都将不胜感激 我的StockLinkedList类目前的基本情况如下: import java.io.FileNotFoundException; impo

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

}
import java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.ObjectOutputStream;
导入java.util.*;
公共类StockLinkedList实现StockList{
LinkedList股票项目列表;
公共股票链接列表(){
stockItemList=新的LinkedList();
}
public void loadStockData(字符串文件名){
试一试{
FileOutputStream stockFile=新的FileOutputStream(文件名+“.dat”);
}catch(filenotfounde异常){
System.out.println(“未找到文件!”);
}
}
public void saveStockData(){
ObjectOutputStream保存=ObjectOutputStream(保存文件);
save.writeObject(objectToSave);
save.close();
}
}

我建议您使用Json序列化。开始使用库的示例

    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));
    }

我建议您使用Json序列化。开始使用库的示例

    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));
    }

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

/**
 * 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;
}
/**
*此方法将把您的股票信息写入文件
*@param path是文件系统中的目标路径
*@param list是您要保存的列表
*@抛出异常
*@抛出FileNotFoundException
*/
public void writeStockInfo(字符串路径,LinkedList列表)引发FileNotFoundException,IOException{
//1.使用文件指向您的文件
文件=新文件(路径);
//2.然后使用OOS,但要序列化为文件,应在调用中使用FileOutputStream
ObjectOutputStream ObjectOutputStream=新的ObjectOutputStream(新文件OutputStream(文件));
//3.只需使用“writeObject”方法将对象写入文件
objectOutputStream.writeObject(列表);
//4.关闭溪流
objectOutputStream.close();
}
/**
*此方法将读取文件中的股票信息
*@param path是您将从中读取信息的文件
*@返回
*@抛出异常
*@抛出FileNotFoundException
*@ClassNotFoundException
*/
公共LinkedList readStockInfo(字符串路径)抛出FileNotFoundException、IOException、ClassNotFoundException{
LinkedList infoList=新建LinkedList();
//1.指向您的文件(您要读取的文件)
File fileToRead=新文件(路径);
//2.使用OIS从使用FileInputStream的文件中读取信息
ObjectInputStream ObjectInputStream=new ObjectInputStream(new FileInputStream(fileToRead));
//3.只需阅读对象并进行转换,以确保获得正确的对象
infoList=(LinkedList)objectInputStream.readObject();
//4.关闭小溪
objectInputStream.close();
返回信息列表;
}
只需确保“StockItem”类正在实现可序列化接口

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

希望这对你有帮助


快乐编码:)

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

/**
 * 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;
}
/**
*此方法将把您的股票信息写入文件
*@param path是文件系统中的目标路径
*@param list是您要保存的列表
*@抛出异常
*@抛出FileNotFoundException
*/
public void writeStockInfo(字符串路径,LinkedList列表)引发FileNotFoundException,IOException{
//1.使用文件指向您的文件
文件=新文件(路径);
//2.然后使用OOS,但要序列化为文件,应在调用中使用FileOutputStream
ObjectOutputStream ObjectOutputStream=新的ObjectOutputStream(新文件OutputStream(文件));
//3.只需使用“writeObject”方法将对象写入文件
objectOutputStream.writeObject(列表);
//4.关闭溪流
objectOutputStream.close();
}
/**
*此方法将读取文件中的股票信息
*@param path是您将从中读取信息的文件
*@返回
*@抛出异常
*@抛出FileNotFoundException
*@ClassNotFoundException
*/
公共LinkedList readStockInfo(字符串路径)抛出FileNotFoundException、IOException、ClassNotFoundException{
LinkedList infoList=新建LinkedList();
//1.指向您的文件(您要读取的文件)
File fileToRead=新文件(路径);
//2.使用OIS从使用FileInputStream的文件中读取信息
ObjectInputStream ObjectInputStream=new ObjectInputStream(new FileInputStream(fileToRead));
//3.只需阅读对象并进行转换,以确保获得正确的对象
infoList=(LinkedList)objectInputStream.readObject();
//4.关闭小溪
objectInputStream.close();
返回信息列表;
}
只需确保“StockItem”类正在实现可序列化接口

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

希望这对你有帮助


快乐编码:)

将其序列化为JSON,然后返回不符合您的需要?可能您误解了使用流的方式。当你想读信息时,你应该使用InputStream,而要写信息,你应该使用OutputStream。@MarceloTataje是的,我也这么认为。我通过谷歌了解了所有这些,发现有些人使用FileOutputStream加载文件,这让我很困惑,但我一点也没有尝试过less@DiversJSON不仅仅是JS吗?这是一个标准的Java控制台应用程序。您需要什么格式来显示您的信息?文本f