Java 读取和比较文本文件行

Java 读取和比较文本文件行,java,file,Java,File,我有一个简单的登录程序,我非常简单的不安全数据库是文本文件 mohammed badpassword1 fahad badpassword2 saad Badpassword3 faisal badpassword4 jack badpasswod5 我让这段代码读取文本文件中的每一行并打印出来 我只想传递用户名并获取下一行的密码,但我不能 我试图将其存储在数组中,并从用户名的下一个元素获取密码,但所有元素都为空,我不知道为什么 我能得到一些帮助吗 public static void mai

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

mohammed
badpassword1
fahad
badpassword2
saad
Badpassword3
faisal
badpassword4
jack
badpasswod5
我让这段代码读取文本文件中的每一行并打印出来

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

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();
    }
}

将当前读取的行与正在查找的行(用户名)进行比较,然后打印下一行

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();
}
}

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

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
}
公共类用户{
私有字符串用户名;
私有字符串密码;
//接球手、二传手、投球手
}
....
公共静态void main(字符串[]args)引发异常{
试一试{
FileReader=newFileReader(“/Users/mohammed/Downloads/testFile.txt”);
BufferedReader BufferedReader=新的BufferedReader(读卡器);
字符串行=新字符串();
//让我们用这个来容纳用户
列表用户=新建ArrayList();
int指数=1;
用户=新用户();
而((line=bufferedReader.readLine())!=null){
现在我们将逐行读取、创建用户对象并将其放入列表中
系统输出打印项次(行);
如果(索引>2){
//表示您正在阅读第三行,因此这是下一个用户的名称
users.add(user);//保存以前的用户
user=new user();//保存新用户的对象
index=1;//重置索引
}
如果(索引==1){
user.setUserName(第行);
}
如果(索引==2){
user.setPassword(行);
}
索引++;
}
reader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
//一旦你有了列表中的所有用户,你就可以做任何你需要的事情
//e、 g.打印所有用户
users.forEach(System.out::println);
//查找您需要的用户,例如,名称为“fahad”
最终用户fahad=users.stream().filter(u->{return u.getUserName().equals(“fahad”)}.findFirst();
System.out.println(fahad.getPassword());//例如
}

您可以使用属性文件。现在使用NIO读取文件。扔掉你的代码,用这一行替换它
List lines=Files.readAllLines(path.get(“myFile.txt”)“我试图将其存储在数组中”-我看不出您在问题的任何地方都尝试过这些。为什么你们要问你们并没有的代码?答案并没有实现“登录”场景。“速度跑得这么快,名声这么好?”jacek cz问题是如何比较字符串,我想是为了了解字符串和文件?我不认为这段代码的目的是让一个真正的登录