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

Posted on 2003-03-24
Medium Priority
Last Modified: 2013-11-24

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",

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");
 currentName = st.nextToken() // ignore next tkn

catch(IOException e) {}

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

Question by:almacal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Author Comment

ID: 8194878
Sorry about the indenting! Should I mention that this is a Java Servlet, and that "list.dat" is a local file?
LVL 30

Accepted Solution

Mayank S earned 300 total points
ID: 8200663
>> Sorry about the indenting

That's what it creates.... compilation errors. I guess you need to have an extra closing brace } before the catch statement for the try block.

Also, I don't think that you'll be able to read and write simultaneously, because since the file is already open for reading, the program will not be able to modify it. I don't think any OS will allow that.


Expert Comment

ID: 9446853
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 
Post your closing recommendations!  No comment means you don't care.
LVL 30

Expert Comment

by:Mayank S
ID: 9451964
Recommendation: Accept mayankeagle's comment as answer. It is complete as per the questioner's needs.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question