How can I replace a line in a file via jtextfield?

It is a program that allows the user to search for an item and then the program breaks down the items and saves them in 4 different ones textfields

. However, below is a method to update the returned items back to a file. The problem is that every time the user hits the refresh button, the line is successfully updated, but other lines in the file are deleted!

I use arraylist

to store the searched item and also a temporary file to write the item and then rename it.

file.txt

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){
    list.add(line);
    linecount++;


    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));
    writer.flush();

    }

    writer.newLine();
    }
    writer.close();
    bf.close();
    filetemp.renameTo(file);
    filetemp.deleteOnExit();


    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());

    }

}
});

      

+3


source to share


1 answer


You only save that data back to the file that contains the line you are looking for and ignore the other lines.

Add this to your code right after the if block

else{
    writer.write(line);
    writer.flush();
}

      

just before the next line:



writer.newLine ();

So your code will look like this:

//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){
    list.add(line);
    linecount++;


    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));
    writer.flush();

    }
    else{
        writer.write(line);
        writer.flush();
    }
    writer.newLine();
    }
    writer.close();
    bf.close();
    filetemp.renameTo(file);
    filetemp.deleteOnExit();


    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());

    }

}
});

      

0


source







All Articles