Hi guys, ran into a small problem, i can read & write anything i want to a text file as well as append to the end of the line but I cant seem to figure out how to edit a specific line.
Lets say in the text file i have:

xml
java
C
assembly

and i want to edit the text file to change the C, into C++, how would I go about doing this? Ive searched through the internet but couldn't find an answer.

my current code is below which only writes to the end of the file & i would like it to modify the 3rd line, can anyone help me as to what i am doing wrong?
thanks

import java.io.*;
import java.util.Scanner;
 
 
public class Testing {
 
 
    public static void main(String[] args) {
 
      try{
           FileReader file1 = new FileReader("/Applications/eclipse/Projects/ATM/src/carlos.txt");
           BufferedReader f = new BufferedReader(file1);
           String temp = null;
           int i =0 ;
           while((temp=f.readLine()) !=null)
           {
             i++;
 
               if(i==3)
               {
            	   try{
                       FileWriter file2 = new FileWriter("/Applications/eclipse/Projects/ATM/src/carlos.txt",true);
                       BufferedWriter file3 = new BufferedWriter(file2);
                  file3.write("C++");
                  
                  file3.close();
             }
        
             catch(Exception e)
             {}
                  }
               
 
 
           }
           f.close();
      }
 
      catch(Exception e)
      {}
 
    }
 
}
text file to change the C, into C++, how would I go about doing this?

are you sure about ...

and about C++, then you probably Win user,

("C:\\Applications\\eclipse\\Projects\\ATM\\src\\carlos.txt");
Comments
Guy probably knows what machine he's running, don't you think?

im a mac user,and the location of my text file is correct.
The problem i am having is editing the text file, the text does not really matter, i just need to modify a line of text in that text file to be something else when i run the code.
so if i have in line 4 Manchester, i run the code and i would want that line to say Liverpool instead and the rest of the text file unchanged. is there a way to do this?

The simplest solution would be to write out a new file with the updated data and delete the original one. A sample implementation to get you started would be: http://www.javadb.com/remove-a-line-from-a-text-file (untested)

A really messed up solution would be to try to get something working with RandomAccessFile but then again it would blow up for variable length substitutions.

The create a new file solution works pretty well for *most* of the cases. If you get the feeling that this solution is turning out to be unwieldy due to performance reasons, move to an in-memory database.

The simplest solution would be to write out a new file with the updated data and delete the original one

Seconded.

Read in the file, modify it in memory, write it out when you're done, overwriting the original. Write out a backup of the original first if you're concerned about losing information. (on a unix machine, call it .FILENAME.bak to hide it)

This article has been dead for over six months. Start a new discussion instead.