0

Here is my txt file look like. admin 12345 funny 123 loop 12390

Hi guys. I am trying to replace particular text in my txt file. For example, I want to replace admin's 12345 with something else that I key in in my input2, it means I want to replace the String(pass) that I find out from txt file through scanner. If I use bufferedwritter, the whole content is going to rewrite..How o solve this problem. I am newbie of programming, kindly need you all help.

    login.addActionListener(this);

public void actionPerformed(ActionEvent e) {

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

       try{

           if(e.getSource()==login)
           {
           Scanner read = new Scanner(new File("oop.txt"));

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

           if(inputUser.equals(user) && inputPass.equals(pass)){    
                  loginTry=false;
                   break;
           }
           }

           if(!loginTry)
           {
                JOptionPane.showMessageDialog(this,"Login Successful");
           }
Lewer Smith
  • 15
  • 1
  • 5
  • If your text file is small, then there is no problem replacing the entire text- Where is the code for writing the file? – Scary Wombat Dec 12 '14 at 02:23
  • What does this have to do with `oop`? – Scott Hunter Dec 12 '14 at 02:24
  • It is one part in my oop programming. For the bufferedwriter and filewriter part, I have clear it since I have no idea how they works. – Lewer Smith Dec 12 '14 at 02:27
  • Check this out. Some pretty efficient ways to do it. http://stackoverflow.com/questions/3935791/find-and-replace-words-lines-in-a-file – Droidman Dec 12 '14 at 05:09
  • Check this out. Some efficient ways to do what you want. http://stackoverflow.com/questions/3935791/find-and-replace-words-lines-in-a-file – Droidman Dec 12 '14 at 05:11

1 Answers1

0

Here is a simple example on how to do what you want!

//Replace a line or word in a file

import java.io.*;

public class BTest
{
 public static void main(String args[])
     {
     try
         {
         File file = new File("file.txt");
         BufferedReader reader = new BufferedReader(new FileReader(file));
         String line = "", oldtext = "";
         while((line = reader.readLine()) != null)
             {
             oldtext += line + "\r\n";
         }
         reader.close();
         // replace a word in a file
         //String newtext = oldtext.replaceAll("drink", "Love");

         //To replace a line in a file
         String newtext = oldtext.replaceAll("This is test string 20000", "blah blah blah");

         FileWriter writer = new FileWriter("file.txt");
         writer.write(newtext);writer.close();
     }
     catch (IOException ioe)
         {
         ioe.printStackTrace();
     }
 }
}

OUTPUT file.txt I drink Java I sleep Java This is test string 1 This is test string 20000

I did both because the way your txt file is you have more than just a word, you have an ID number of some sorts right next to your users login information. So i would use change line!