【问题标题】:Java Isolate comma seperated values in text fileJava隔离文本文件中的逗号分隔值
【发布时间】:2018-04-29 22:51:29
【问题描述】:

我一直在寻找解决问题的方法,但没有任何运气。所以现在我在这里寻求帮助。

我正在通过以下课程创建“组”:

public class Group {
   private String groupID;
   private ArrayList<User> usersInGroup;

User 类如下所示: 注意:我已经有一个包含所有现有用户的 ArrayList。

public class User {
   private String firstName;
   private String lastName;
   private String age;
   private String gender;
   private String usernameID;
   private String password;

我已经从“groupData.txt”CSV 文本文件中添加了 groupID 字段,如下所示:

public static ArrayList<Group> listOfCreatedGroups() throws IOException {
    ArrayList<Group> listOfGroups = new ArrayList<>();

    FileReader fr = new FileReader("src/groupData.txt");
    BufferedReader bfr = new BufferedReader(fr);
    String line;

    int totalLine = Destination.linesInFile("src/groupData.txt"); //total lines in file

    for (int i = 0; i < totalLine; i++) {
        line = bfr.readLine();
        String[] groupID = line.split(",");
        Group temp = new Group();

        temp.setGroupID(groupID[0]);

        listOfGroups.add(temp);
    }
    try {
        bfr.close();
        fr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return listOfGroups;
}

“groupData.txt”文件的结构如下:

行示例 = groupID,String_1,String2,String3 ... Stringn,\n

groupid,user,user,user,user,user,user,user,

groupid,user,user,user,

groupid,user,user,user,user,user

组ID,用户,用户

groupid,user,user,user,user

由于我在文本文件中只有每个组 User.usernameID 中的用户数为 1 到 n 个字符串,因此我无法将整个用户对象添加到 Arraylist usersInGroup。 我需要以某种方式隔离 usernameID 并找到相应的用户并将它们添加到 ArrayList usersInGroup。

我希望你们中的任何人都可以给我一个正确方向的提示。谢谢。

【问题讨论】:

  • 你能发布一个你的 csv 的多行示例以及类实例应该是什么
  • 什么是用户粘贴示例,你有一个完整的用户类,但没有数据可以进入
  • 是的。现在在帖子中添加了一个多行示例。实例也如帖子中所示。在显示的“listOfCreatedGroups()”方法中,我想要一个所有创建组的列表。每个组由文本文件中的每一行组成。
  • 对不起。我已经有一个包含所有现有用户的 ArrayList。每个用户都以类似的方式创建 - 也来自文本文件。

标签: java csv arraylist


【解决方案1】:

我不知道这是否是您想要的,因为我没有找到非常具体的用户数据,甚至没有提到您想要的方式。但是让我知道这是否足够

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;

    public class Group {
        private static String groupID;
        private static ArrayList<User> usersInGroup = new ArrayList<User>();

    public static void main (String [] args) throws IOException {
        addListToGroup(readFile());
    }
    public static void addListToGroup(ArrayList<ArrayList<String>> list) {
        for (int i = 0; i < list.size(); i++) {
            groupID = list.get(i).get(0);
            for (int x = 0; x < list.get(i).size(); x++) {
                User temp = new User(); // change this to however you setup the txt file 
                // the information from the list is in list.get(i).get(x) in order as in the textfile
                temp.setAge(null);
                temp.setFirstName(null);
                temp.setGender(null);
                temp.setLastName(null);
                temp.setPassword(null);
                temp.setUsernameID(null);
                usersInGroup.add(temp);
            }
        }
    }
    public static ArrayList<ArrayList<String>> readFile() throws IOException {
        List<String> temp = new ArrayList<String>();
        Path path = Paths.get("file.txt");
        temp = Files.readAllLines(path);
        ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
        for (int i = 0; i < temp.size(); i++) {
            String [] s = temp.get(i).split(",");
            ArrayList<String> quickArray = new ArrayList<String>();
            for (int x=0; x < s.length; x++) {
                quickArray.add(s[x]);
            }
            lines.add(quickArray);
        }
        return lines;
    }

}
class User {
    private String firstName;
    private String lastName;
    private String age;
    private String gender;
    private String usernameID;
    private String password;
    //setters and getters
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getUsernameID() {
        return usernameID;
    }
    public void setUsernameID(String usernameID) {
        this.usernameID = usernameID;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

【讨论】: