-1

I am creating one application in Java swing api. I am using txt file as my database to store,update etc. I completed storing procedure successfully. But I cant update .If I click update button on my application the data in file could not be replaced. can you please tel me how to replace a data in file?

Thanks in advance...

import java.io.*;

public class test {

    public static void main(String args[]) {
        try {
            String data = null;
            File file = new File("student.txt", true);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            while ((data = br.readLine()) != null) {
                String[] de = data.split(" ");
                if (de[0].equals("vimal")) {
                    data.trim();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81
Vijay Kumar
  • 27
  • 1
  • 4
  • 9
  • Please share your code with us. – Shashank Kadne Feb 01 '13 at 12:30
  • 2
    have you red the [faq](http://stackoverflow.com/faq)? also, if posting code, please post an [SSCCE](http://sscce.org/) – linski Feb 01 '13 at 12:32
  • You'll probably realize that by only telling _I can't update_, or _data in the file could not be replaced_, it's exceedingly unlikely that someone can reasonably answer this question. Can you please provide more details? – Xavi López Feb 01 '13 at 12:33
  • As `java.io.File` has no such constructor; this example fails to compile. – trashgod Feb 01 '13 at 12:41
  • 1
    What's the swing tag for? I can't see a single Swing-UI element in your code. – Guillaume Polet Feb 01 '13 at 12:48
  • See this response , might help you [Java - delete line from text file by overwriting while reading it][1] [1]: http://stackoverflow.com/a/6477893/945317 – SomeCode.NET Feb 01 '13 at 13:19

1 Answers1

0

you need to close your buffered reader after all and in your fileReader need an "True"

 import java.io.*;
 public class test {
   public static void main(String args[]) {
    try {
      String data= null;
      File file=new File("student.txt");
      FileReader fr =new FileReader(file,true);
      BufferedReader br = new BufferedReader(fr);
      while((data=br.readLine())!= null) {
        String[] de = data.split(" "); 
        if(de[0].equals("vimal")) {
           data.trim();
        }
      }
    } catch (IOException e) {
        e.printStackTrace();
    }
    br.close()
  }
 }
Amarnath
  • 8,736
  • 10
  • 54
  • 81