-1

I am newbie in the java programming. Here I got a question since I don't know how to change the text in the text file. How to change the String pass in notepad. I am trying to insert a new password in password field which in order to change(pass) that is password in my text file.

public void actionPerformed(ActionEvent e) {

    String inputUser = input1.getText();
    String inputPass = input2.getText();
    File loginf = new File("oop.txt");

    if(e.getSource() == returnLP) {
        TrialGUI link = new TrialGUI();
        dispose();
    }

    try {       
        if(e.getSource()==reset) {     
               FileWriter fstream = new FileWriter("oop.txt");
               BufferedWriter out = new BufferedWriter(fstream);
               Scanner read = new Scanner(new File("oop.txt"));

               while(read.hasNext()) {
                   String user = read.next();
                   String pass = read.next();

                   if(inputUser.equals(user)){                     
                      break;
                   }
               }

               read.close();
         } catch(Exception e) {
               e.printStackTrace();
         }
}
Charlie
  • 978
  • 1
  • 7
  • 27
Lewer Smith
  • 15
  • 1
  • 5

1 Answers1

1

If you have a file of the format:

name=abc

password=xyz

then I would suggest that you may find loading the file to a java.util.Properties instance easier to work with.

Once loaded you can simply update any properties as required and then write back out your Properties instance once finished.

There are complete code samples here on how to read and write properties files.

http://www.mkyong.com/java/java-properties-file-examples/

Your code will look something like:

if(e.getSource() == reset) {    
    Properties props = new Properties();
    InputStream in = new  FileInputStream("oop.txt")
    props.load(in);
    in.close();

    //you can now access existing user name and password using proper.getProperty(key);

    //update to new details
    props.setProperty("user", inputUser);
    props.setProperty("password", inputPass);

    OutputStream out = new FileOutputStream("oop.txt");
    props.store(out, null);
    out.flush();
    out.close();

 }
Alan Hay
  • 22,665
  • 4
  • 56
  • 110