Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

[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
0
almacal
Asked:
almacal
  • 2
1 Solution
 
almacalAuthor Commented:
Sorry about the indenting! Should I mention that this is a Java Servlet, and that "list.dat" is a local file?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.

Mayank.
0
 
CleanupPingCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Recommendation: Accept mayankeagle's comment as answer. It is complete as per the questioner's needs.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now