读取和比较文本文件行

时间:2018-04-28 08:32:51

标签: java file

我有一个简单的登录程序,我非常简单的不安全数据库是文本文件,就是这个

mohammed
badpassword1
fahad
badpassword2
saad
Badpassword3
faisal
badpassword4
jack
badpasswod5

我有这段代码读取文本文件中的每一行并打印出来

我只想传递用户名并获取密码,这是下一行,但我不能 我试图将它存储在数组中并从用户名中获取下一个元素的密码,但所有元素都是NULL,我不知道为什么 我可以得到一些帮助吗?

public static void main(String[] args) throws Exception{

    try {
        FileReader reader = new FileReader("/Users/mohammed/Downloads/testFile.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);

        String line = new String();

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

我知道这可能是一个有点愚蠢的解决方案,但它应该可行。

public class User {
    private String username;
    private String password;

    // getters, setters, toString
}

....

public static void main(String[] args) throws Exception{

    try {
        FileReader reader = new FileReader("/Users/mohammed/Downloads/testFile.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);

        String line = new String();

        // let's have this one to hold users
        List<User> users = new ArrayList<>();

        int index = 1;
        User user = new User();
        while ((line = bufferedReader.readLine()) != null) {
            now we will read line by line, create User objects and put them into the list
            System.out.println(line);
            if (index > 2) {
                // means you are reading the third line, so that's the name of the next user
                users.add(user); // save previous user
                user = new User(); // object to hold new one
                index = 1; // reset index
            }
            if (index == 1) {
                user.setUserName(line);
            }
            if (index == 2) {
                user.setPassword(line);
            }
            index++;
        }
        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    // and once you have all the users in the list you can do whatever you need
   //e.g. print all users
   users.forEach(System.out::println);

   // find the user you need, e.g. with name 'fahad'
   final User fahad = users.stream().filter(u -> { return u.getUserName().equals("fahad")}).findFirst();
   System.out.println(fahad.getPassword()); // for instance
}

答案 1 :(得分:-1)

将您当前阅读的行与您要查找的行(用户名)进行比较并打印下一行

public static void main(String[] args) throws Exception{

try {
    FileReader reader = new FileReader("/Users/mohammed/Downloads/testFile.txt");
    BufferedReader bufferedReader = new BufferedReader(reader);
    String username="mohammed";
    String line = new String();

    while ((line = bufferedReader.readLine()) != null) 
        if(line.equals(username){
            System.out.println(bufferedReader.readLine());
            break;
        }

    reader.close();

} catch (IOException e) {
    e.printStackTrace();
}
}