如何在java中将文本文件读取到不同对象类型的ArrayList?

如何在java中将文本文件读取到不同对象类型的ArrayList?,java,object,arraylist,text-files,Java,Object,Arraylist,Text Files,我是一个编程初学者,我需要一些帮助来尝试将文本文件读入包含不同类型对象的ArrayList。我已经创建了一个程序来控制视频的目录,目前我已经将其硬编码到程序中的ArrayList中,但我想更改它,以便每次程序运行时,程序从包含资源清册的文本文件中读取,并将其转换为ArrayList,而不是从程序中已有的ArrayList中读取。我已经添加了一个函数,在程序退出后将清单写入文本文件,但我似乎无法从文本文件读取清单 我遇到的问题是,我的ArrayList(视频)包含(字符串、字符串、字符、字符串)。

我是一个编程初学者,我需要一些帮助来尝试将文本文件读入包含不同类型对象的ArrayList。我已经创建了一个程序来控制视频的目录,目前我已经将其硬编码到程序中的ArrayList中,但我想更改它,以便每次程序运行时,程序从包含资源清册的文本文件中读取,并将其转换为ArrayList,而不是从程序中已有的ArrayList中读取。我已经添加了一个函数,在程序退出后将清单写入文本文件,但我似乎无法从文本文件读取清单

我遇到的问题是,我的ArrayList(视频)包含(字符串、字符串、字符、字符串)。我不知道如何更改现有的代码,以便scanner将文本文件中的每一行拆分为适当的块(标题、类型、可用性和返回日期),然后将每个块插入ArrayList中的适当位置。我希望这是有道理的

我尝试过创建CSV并使用split()函数,但我不知道如何使用它插入ArrayList,因为我在一行中有四个字符串,而不是(字符串、字符串、字符、字符串)。我甚至尝试更改当前的ArrayList,使每个元素都是一个字符串,但我仍然不确定如何使其工作

任何帮助都将不胜感激。如果您需要更多信息,请告诉我

编辑:总而言之,我的问题是:如果我有一个如下所示的文本文件,我如何将其拆分为4行,然后将每行拆分为4个字符串(或3个字符串和1个字符),并将每个字符串插入ArrayList,这样我就得到了一个包含4个InventoryRow的ArrayList,如下所示: (“卡萨布兰卡”,“旧”,“Y”,空)

库存行类别:

class InventoryRow {
private String name;
private String type;
private Character availability;
private String returndate;

public InventoryRow(String name, String type, Character availability,
        String returndate) {
    this.name = name;
    this.type = type;
    this.availability = availability;
    this.returndate = returndate;
}

public String getReturndate() {
    return returndate;
}

public void setReturndate(String returndate) {
    this.returndate = returndate;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Character getAvailability() {
    return availability;
}

public void setAvailability(Character availability) {
    this.availability = availability;
}

public String toString() {
    return name + "   " + type + "   " + availability + "   " + returndate;
}
}
主要方法(包括我当前无法使用的代码):


看起来您正在尝试进行基本的序列化和反序列化。 我将关注您的while(run)循环,以便您能够从该文件填充ArrayList。InventoryRow类足够好,数组列表参数化正确

//This creates an object to read the file
Scanner s = new Scanner(new File("videos.txt"));


while (s.hasNext()) {
   //This is where the problem is:
   videos.add(new InventoryRow(s.next(), null, null, null));
}
s、 next()将返回如下字符串: “name;type;a;date”您需要通过执行以下操作在分隔符上拆分此字段:

String line = s.next();
String[] fields = line.split(","); //use your choice of separator here & check how to use the split method.

使用获得的字段创建InventoryRow对象,然后将其添加到while循环中的ArrayList中。除非您特别希望可用性是一个字符,否则可以将其保留为字符串

看起来您正在尝试进行基本的序列化和反序列化。 我将关注您的while(run)循环,以便您能够从该文件填充ArrayList。InventoryRow类足够好,数组列表参数化正确

//This creates an object to read the file
Scanner s = new Scanner(new File("videos.txt"));


while (s.hasNext()) {
   //This is where the problem is:
   videos.add(new InventoryRow(s.next(), null, null, null));
}
s、 next()将返回如下字符串: “name;type;a;date”您需要通过执行以下操作在分隔符上拆分此字段:

String line = s.next();
String[] fields = line.split(","); //use your choice of separator here & check how to use the split method.

使用获得的字段创建InventoryRow对象,然后将其添加到while循环中的ArrayList中。除非您特别希望可用性是一个字符,否则可以将其保留为字符串

看起来您正在尝试进行基本的序列化和反序列化。 我将关注您的while(run)循环,以便您能够从该文件填充ArrayList。InventoryRow类足够好,数组列表参数化正确

//This creates an object to read the file
Scanner s = new Scanner(new File("videos.txt"));


while (s.hasNext()) {
   //This is where the problem is:
   videos.add(new InventoryRow(s.next(), null, null, null));
}
s、 next()将返回如下字符串: “name;type;a;date”您需要通过执行以下操作在分隔符上拆分此字段:

String line = s.next();
String[] fields = line.split(","); //use your choice of separator here & check how to use the split method.

使用获得的字段创建InventoryRow对象,然后将其添加到while循环中的ArrayList中。除非您特别希望可用性是一个字符,否则可以将其保留为字符串

看起来您正在尝试进行基本的序列化和反序列化。 我将关注您的while(run)循环,以便您能够从该文件填充ArrayList。InventoryRow类足够好,数组列表参数化正确

//This creates an object to read the file
Scanner s = new Scanner(new File("videos.txt"));


while (s.hasNext()) {
   //This is where the problem is:
   videos.add(new InventoryRow(s.next(), null, null, null));
}
s、 next()将返回如下字符串: “name;type;a;date”您需要通过执行以下操作在分隔符上拆分此字段:

String line = s.next();
String[] fields = line.split(","); //use your choice of separator here & check how to use the split method.

使用获得的字段创建InventoryRow对象,然后将其添加到while循环中的ArrayList中。除非您特别希望可用性是一个字符,否则可以将其保留为字符串

您可能需要这样的东西:

List<InventoryRow> videos = new ArrayList<InventoryRow>();
while (s.hasNextLine()) {
    String[] split = s.nextLine().split("   ");
    // TODO: make sure the split has correct format

    // x.charAt(0) returns the first char of the string "x"
    videos.add(new InventoryRow(split[0], split[1], split[2].charAt(0), split[3])); 
}
List videos=new ArrayList();
而(s.hasNextLine()){
字符串[]split=s.nextLine().split(“”);
//TODO:确保拆分的格式正确
//x.charAt(0)返回字符串“x”的第一个字符
添加(新的清单行(拆分[0],拆分[1],拆分[2]。字符(0),拆分[3]);
}

您可能需要这样的东西:

List<InventoryRow> videos = new ArrayList<InventoryRow>();
while (s.hasNextLine()) {
    String[] split = s.nextLine().split("   ");
    // TODO: make sure the split has correct format

    // x.charAt(0) returns the first char of the string "x"
    videos.add(new InventoryRow(split[0], split[1], split[2].charAt(0), split[3])); 
}
List videos=new ArrayList();
而(s.hasNextLine()){
字符串[]split=s.nextLine().split(“”);
//TODO:确保拆分的格式正确
//x.charAt(0)返回字符串“x”的第一个字符
添加(新的清单行(拆分[0],拆分[1],拆分[2]。字符(0),拆分[3]);
}

您可能需要这样的东西:

List<InventoryRow> videos = new ArrayList<InventoryRow>();
while (s.hasNextLine()) {
    String[] split = s.nextLine().split("   ");
    // TODO: make sure the split has correct format

    // x.charAt(0) returns the first char of the string "x"
    videos.add(new InventoryRow(split[0], split[1], split[2].charAt(0), split[3])); 
}
List videos=new ArrayList();
而(s.hasNextLine()){
字符串[]split=s.nextLine().split(“”);
//TODO:确保拆分的格式正确
//x.charAt(0)返回字符串“x”的第一个字符
添加(新的清单行(拆分[0],拆分[1],拆分[2]。字符(0),拆分[3]);
}

您可能需要这样的东西:

List<InventoryRow> videos = new ArrayList<InventoryRow>();
while (s.hasNextLine()) {
    String[] split = s.nextLine().split("   ");
    // TODO: make sure the split has correct format

    // x.charAt(0) returns the first char of the string "x"
    videos.add(new InventoryRow(split[0], split[1], split[2].charAt(0), split[3])); 
}
List videos=new ArrayList();
而(s.hasNextLine()){
字符串[]split=s.nextLine().split(“”);
//TODO:确保拆分的格式正确
//x.charAt(0)返回字符串“x”的第一个字符
添加(新的清单行(拆分[0],拆分[1],拆分[2]。字符(0),拆分[3]);
}

不确定您的问题是什么。。使用
扫描仪
读取4次?或者通读整行,然后读一首诗