Link to home
Start Free TrialLog in
Avatar of almacal
almacal

asked on

[Java] Overwriting a line in a plain textfile with FileWriter

Hi,

Is it possible to find a line in a text file, and then overwrite that line with something else, or delete it altogether ? Here's some code

// set up file readers and writers
FileReader fRead = new FileReader("list.dat");
FileWriter fWrite = new FileWriter("list.dat",
                                    true);

BufferedReader bIn = new BufferedReader(fRead);
BufferedWriter bOut = new BufferedWriter(fWrite);

String line, currentName;

// userName is from a html form
try {
while((line = bIn.readLine()) != null) {
 StringTokenizer st = new StringTokenizer(line);
 currentName = st.nextToken();
 if(currentName.compareTo(userName) == 0) {
  bOut.write(userName + "Expired");
  break;
 }
 currentName = st.nextToken() // ignore next tkn
}

catch(IOException e) {}

Am I way off base here, or what? Any help is appreciated. thanks

Alex
Avatar of almacal
almacal

ASKER

Sorry about the indenting! Should I mention that this is a Java Servlet, and that "list.dat" is a local file?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
almacal:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Recommendation: Accept mayankeagle's comment as answer. It is complete as per the questioner's needs.