Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read line by line by using FileReader

Thank you for your attention.

I have created a program which I'm using a Login form and Register form. Once users register their email and their password will be saved it into submit.txt. Then they will turn back to login form and enter their email and password which are saved into submit.txt.

In my code, I'm using write file for Register form, and Read file for Login Form. But, it doesn't work. I know my problem in the code used for Read File. May you help me to realize that?.

Thank you too much for your help.

 if (checkPassword(usern, hash)) {
    System.out.println("Logged in!");
    ChooseWindow ptb = new ChooseWindow();
    ptb.setVisible(true);
    LoginWindow.this.dispose();
 } else {
    System.out.println("Wrong password");
 }

 public boolean checkPassword(String username, String pass) {
    try {
        FileReader inFile = new  FileReader("/users/Ender/Desktop/GetUser/submit.txt");
        BufferedReader inStream = new BufferedReader(inFile);
        String inString;

        while ((inString = inStream.readLine()) != null) {}
        inStream.close();                       
    }catch (IOException e) {
        e.printStackTrace();
    }
    return false;
 }
like image 747
Ender phan Avatar asked Nov 24 '15 11:11

Ender phan


People also ask

Does FileReader read line by line?

txt is read by FileReader class. The readLine() method of BufferedReader class reads file line by line, and each line appended to StringBuffer, followed by a linefeed. The content of the StringBuffer are then output to the console.

How do you scan a line by line in Java?

To read the line and move on, we should use the nextLine() method. This method advances the scanner past the current line and returns the input that wasn't reached initially. This method returns the rest of the current line, excluding any line separator at the end of the line.


1 Answers

here is my code to read from a file :

 String line;

    try {

        BufferedReader bufferreader = new BufferedReader(new FileReader("C:\\Users\\ahmad\\Desktop\\aaa.TXT"));


        while ((line = bufferreader.readLine()) != null) {     
          /** 
            Your implementation  
          **/

        }

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
like image 196
Ahmad Alkhatib Avatar answered Sep 27 '22 23:09

Ahmad Alkhatib