简体   繁体   English

如何将带分隔符的文本文件读入数组并将数组元素存储在变量中?

[英]How to read textfile with delimiter into array and store array elements inside a variable?

I was trying to make a login page where I try to import the user's data into an array.我试图创建一个登录页面,在其中尝试将用户的数据导入数组。 Then match the username and password entered with the username(array[1]) and password(array[2] stored in the array. Please help me.然后将输入的用户名和密码与存储在数组中的用户名(数组[1])和密码(数组[2])匹配。请帮助我。

Text File:文本文件:

72|shawn31|123456|Shawn 
Brown|KL|shwanbrown@gmail.com|Administrator|01234567|1|1|1970

Code:代码:

public void openFile() {
    try {
        readCodes = new Scanner(new File("C:\\Users\\april\\Documents\\NetBeansProjects\\OODJ\\user.txt"));
    } catch (Exception e) {
        System.out.println("Could not locate the data file");
    }
}

public void readFile() {
    while (readCodes.hasNextLine()) {
        String d = readCodes.nextLine();
        StringTokenizer st = new StringTokenizer(d, "|");
        String[] userDetails = new String[st.countTokens()];
        int index = 0;
        while (st.hasMoreTokens()) {
            userDetails[index] = (String) st.nextElement();
            index++;
        }
    }
    userDetails[0] = id;
    userDetails[1] = username;
    userDetails[2] = password;
    userDetails[3] = name;
    userDetails[4] = address;
    userDetails[5] = email;
    userDetails[6] = role;
    userDetails[7] = number;
    userDetails[8] = day;
    userDetails[9] = month;
    userDetails[10] = year;
}

public boolean login(String uname, String pass) {
    boolean success = false;
    readFile();
    if (userDetails[1].equals(uname) && userDetails[2].equals(pass)) {
        success = true;
    }
    return success;
}

The main problem is you do not assign array values to the fields, but vice versa.主要问题是您没有将数组值分配给字段,反之亦然。

public void readUser() {
    try {
        Path path = Paths.get("C:\\Users\\april\\Documents\\NetBeansProjects\\OODJ\\user.txt");
        Files.lines(path, Charset.defaultCharset())
                .map(line -> line.split("\\|"))
                .filter(arr -> arr.length == 11)
                .limit(1)
                .forEach(arr -> {
                    id = arr[0];
                    username = arr[1];
                    password = arr[2];
                    name = arr[3];
                    address = arr[4];
                    email = arr[5];
                    role = arr[6];
                    number = arr[7];
                    day = arr[8];
                    month = arr[9];
                    year = arr[10];
                });
    } catch (Exception e) {
        System.out.println("Could not locate the data file");
    }
}

An idea to make it a bit more classy, extending upon it:一个让它更优雅的想法,扩展它:

class User {
    String id;
    String username;
    String password;
    String name;
    String address;
    String email;
    String role;
    String number;
    String day;
    String month;
    String year;
}

public List<User> readUsers() throws IOException {
    Path path = Paths.get("C:\\Users\\april\\Documents\\NetBeansProjects\\OODJ\\users.txt");
    return Files.lines(path, Charsets.defaultCharset())
            .map(line -> line.split("\\|"))
            .filter(arr -> arr.length == 11)
            .map(arr -> {
                User user = new User();
                user.id = arr[0];
                user.username = arr[1];
                user.password = arr[2];
                user.name = arr[3];
                user.address = arr[4];
                user.email = arr[5];
                user.role = arr[6];
                user.number = arr[7];
                user.day = arr[8];
                user.month = arr[9];
                user.year = arr[10];
                return user;
            })
          .collect(Collectors.toList());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM