-1

I am making a log in gui that read from 2 text file. The username line matches the same line as in password. If username is in line 3 then password will be in line 3 in password.txt If the username and password matches from the username.txt and password.txt, the system will log you in. I am able to get the specific line number from the username text but I keep getting null when password.txt is read.

I tried using scanner but i dont need to read every line. i just need to read the specific line.

private class ButtonHandler implements ActionListener  
    {
        public void actionPerformed (ActionEvent event) 
        {
            try {
                File f1=new File("username.txt");
                File f2=new File ("password.txt");
                String[] words=null; 
                FileReader fr1 = new FileReader(f1);
                FileReader fr2 = new FileReader(f2);  
                BufferedReader br1 = new BufferedReader(fr1);
                BufferedReader br2 = new BufferedReader(fr2);
                String s;
                String user = username.getText();
                String pass=String.valueOf(password.getPassword());
                String usertxt = " ";
                String passtxt=" ";
                int count =0;
                  while((s=br1.readLine())!=null)   //Reading Content from the file
                  {
                     words=s.split(" ");  //Split the word using space
                      for (String word : words) 
                      {
                             if (word.equals(user))   //Search for the given word
                             {

                                 System.out.print(count);

                                    for (int i = 0; i < count; i++)
                                    {
                                        br2.readLine();
                                        passtxt = br2.readLine();
                                    }
                                    System.out.print(passtxt);
                                    if(pass.equals(passtxt))
                                        JOptionPane.showMessageDialog(null,"User has logged in");                                           
                             }
                      }
                      count++;
                  }       
                }
            catch (IOException e) 
                {               
                System.out.println("Uh oh, got an IOException error!");
                e.printStackTrace();            
                }   
            }
        }

I want the string pass to equal passtxt.

  • At which part of your function is "all I am getting is null" occurring? Do you mean you are getting a NullPointerException? If so, which line is it occurring at? – GMc Jul 15 '19 at 07:42

1 Answers1

0

If you know which byte position the line starts at you can use a RandomAccessFile and seek to that position in the file.

The documentation for RandomAccessFile methods (seek) allow you to position the read pointer exactly where you need it. Then you can use a read method to get the data from the file.

However, this probably isn't going to work for you because the third line will almost certainly start at a random location in your file. The random location is because the previous two lines will be variable in length.

So unless you read the file to determine where line 3 starts, random access is if little use in this problem.

If your file is in the form of a properties file (for example) or some other structure (json etc) then you could always use a library to help you read the content of the file and extract the data you need.

You might refer to the Properties.load method for an example of how to load properties from a file.

Note that using a library to access the file will probably result in the entire file being read within the library. So if your goal is to minimize your I/O and your input file is large you might not achieve that goal.

IMHO, the simplest approach is to read and discard the first lines until you get to the line(s) you want and then process them

GMc
  • 1,764
  • 1
  • 8
  • 26