How to replace a line in a file through jtextfield?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: How to replace a line in a file through jtextfield?

  1. #1
    Join Date
    Jan 2013

    How to replace a line in a file through jtextfield?

    This is a program that allows the user to search for an item and then the program splits the items and stores them in 4 different textfields. However, below is a method for updating the retrieved items back to the file. The problem is whenever the user clicks on the update button, the line does update successfully, but the other lines in the file are deleted!

    Iam using an arraylist to store the searched item and also temporary file to write the item and then renaming it.

    Can someone help me please. Thanks in advance!

    ramal hotel1  10 uk
    bren  hotel2  20 france
    gil   hotel3  30 china

    //Update to file
    button9.addActionListener(new ActionListener(){
    public  void actionPerformed(ActionEvent ae){
        try {
        String stringSearch = textfield1.getText();
        File filetemp = File.createTempFile("TempFileName", ".tmp", new File("/"));
        BufferedWriter writer = new BufferedWriter(new FileWriter(filetemp));
        File file = new File("file.txt");
        BufferedReader bf = new BufferedReader(new FileReader(file));
        int linecount = 0;
                  String line;
        ArrayList<String> list = new ArrayList<String>();
        while (( line = bf.readLine()) != null){
        int indexfound = line.indexOf(stringSearch);
        if (indexfound > -1) {
        String a = textfield1.getText();
        String b = textfield2.getText();
        String c = textfield3.getText();
        String d = textfield4.getText();
        String[] word = line.split("\t");
        String firstword = word[0];
        String secondword = word[1];
        String thirdword = word[2];
        String fourthword = word[3];
        writer.write(line.replace("\t", "\t").replace(firstword, b).replace(secondword, c).replace(thirdword, d));
        FileChannel src = new FileInputStream(filetemp).getChannel();
        FileChannel dest = new FileOutputStream(file).getChannel();
        dest.transferFrom(src, 0, src.size());
        }catch (IOException e) {
        System.out.println("IO Error Occurred: " + e.toString());

  2. #2
    Join Date
    May 2006

    Re: How to replace a line in a file through jtextfield?

    Your program is only outputting the values in the text fields and so the rest of the file will be lost. You need to write out the values for the whole file.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center