有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在一行中读取文件中的数字(ID),以逗号分隔

我有两个文本文件。txt和存储。txt

物品。txt是这样组织的(行是ID、名称、类别)

一,

老人与海

文学作品

二,

面包

食物

等等

还有商店。txt的组织方式如下(行为ID、名称、城市、项目ID)

一,

商店1

伦敦

2,3,4,5,6

我不知道如何用商店里的商品ID来阅读这一行。txt,以便将它们添加到存储区内的列表/集合中

这就是商店的样子:

public Store(String name, long id, String city, Set<Item> items) {
        super(name, id);
        this.city = city;
        this.items = items;

    }

我已经为项目创建了一个函数


 private static void readItemsFromFile (List <Item> items){

        try (FileReader fileReader = new FileReader(Main.ITEMS_FILE_NAME);
             BufferedReader reader = new BufferedReader(fileReader)) {

            String line;

            while ((line = reader.readLine()) != null) {
                Long id = Long.parseLong(line);
                String name = reader.readLine();
                String categoryLine=reader.readLine();

            new Item(id, name, category);

            items.add(item);

           }
        } catch (IOException e) {
            System.out.println("File " + Main.CATEGORIES_FILE_NAME + " not found.");
            logger.error(e.getMessage(), e);
        }

    }

但我不确定如何解决商店的问题


共 (0) 个答案