The Artima Developer Community
Sponsored Link

Java Answers Forum
Delete/edit/add to a text file or recreate?

4 replies on 1 page. Most recent reply: Sep 8, 2009 6:56 PM by shane lancaster

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Delete/edit/add to a text file or recreate? Posted: Apr 1, 2005 1:14 AM
Reply to this message Reply
Advertisement
Coding Style: NetBeans IDE 4.0 Beta2 (Java)

My application works with a text file (Info.txt) which stores information about users (name, IP-Address, Status), everything is delimited with SPACES.
I need to be able to manipulate the file to either:
- Edit the STATUS of a user (between ON and OFF)
- Add a new Line (new user information)
- Delete a line (remove user information)

Is there an easy way to manipulate Text Files so that I can accomplish such a task (like using a TABLE? something similar?)
Or should I just rename (temp.txt), recreate, delete (temp.txt)?
Thanks,


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Delete/edit/add to a text file or recreate? Posted: Apr 4, 2005 4:25 AM
Reply to this message Reply
You's is a basic db application.
For the status field you could edit the file (if ON and OFF have the same length, f.e. 1 and 0), adding lines would be no problem.
But since you want to delete lines, you have to read the whole file and write the whole file.

Textfiles are nothing like tables, they don't even have a fix line length.


1. Read the whole file in a Vector of lines.
String[] would be better (String[][] would be even better, isnce this way you would have a table), but it makes it mor complicate to add something.

2. Manipulate the data

3. Create a new FileWriter, setting the append option to false.

4. Insert the lines

Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Re: Delete/edit/add to a text file or recreate? Posted: Apr 6, 2005 1:10 AM
Reply to this message Reply
My problem is the FileWriter recreates the source file I am reading every time.

My goal is to edit a Text File, so far the only solution I have found is to OPEN the current Text File (FileReader to Chatter.txt) and create a new Text File (FileWriter to oChatter.txt), .readline all the lines that do not need changing (Catch the ones I do and edit them) and write
each line into the new text file (as shown in the code below)


String incomming = "";

File fSourceStore = new File("Chatter.txt");
File fDestStore = new File("oChatter.txt");
FileReader frStore = new FileReader(fSourceStore);
FileWriter fwStore = new FileWriter(fDestStore);
BufferedReader brStore = new BufferedReader(frStore);
BufferedWriter bwStore = new BufferedWriter(fwStore);

boolean bSearch = true;
while (bSearch == true)
{
incomming = brStore.readLine();
if (incomming == null)
{
// End of File
bwStore.close();
brStore.close();
frStore.close();
fwStore.close();
bSearch = false;
}
else if (DETERMINE IF THIS LINE NEEDS TO BE EDITED)
{
// EDIT THE LINE //
bwStore.write(newLine);
bwStore.newLine();
}
else
{
// Keep information in file
bwStore.write(incomming);
bwStore.newLine();
}
}
[CODE]

This works but I can't seem to figure out how to then delete the current Chatter.txt and rename oChatter.txt to Chatter.txt.

But isn't there a way to OVERWRITE or MODIFY the file and not have to create/rename a TEMP file (oChatter.txt) every time I want to make a slight change to the file?
There has to be like a ReaderWriter or some .Overwrite or something? Do I have to read the entire file into memory then write it afterwards (thus allowing it to overwrite the file?)

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Delete/edit/add to a text file or recreate? Posted: Apr 6, 2005 9:53 AM
Reply to this message Reply
Textfiles don't support text editing.
It is impossible to change a "line", because "lines" don't exist. They are an abstract concept. A Textfile is a collection of chars with some CR (return) chars in them.

A FileWriter in combination with a BufferedWriter can only append Strings to a file, it can't write specific lines.
You can tell the FileWriter to first delete the old file and create a new one or to append to the existing file.
A BufferedWriter is not able to change specific bytes inside a file.

Q: Why this limitation?
A: Standard Textfiles don't have fixed length lines.

Q: So what's the problem?
A: Try to imagine the following scenarios:
1. The new line is shorter then the old one
causes: An additional CR char in the line, wich means that you actually add a new line. The original line contains the new content, the inserted line contains the last chars of the old line. The only solution would be again to recreate the whole file after this line.
2. The new line is longer then the old one
In this case you overwrite the first chars of the following line. The only solution would be again to recreate the whole file after this line.

It would be very slow to recreate the file everytime you change a line. That's why the standard BufferedWriter does not support writing specific lines.

What you need to do?
1. Open a file reader
2. Read the whole file content in a String[] object or a Vector.
3. close the file reader
3. change the lines wich need to be changed in this String[] object
4. open a filewriter (append = false)
write the content of your String[] or Vector into this filewriter
5. close the filewriter.

If you want to use a binary writer instead of the BufferedWriter, make sure your new line allways has the length of the old line or bring all lines to the same length, adding spaces after the line like they do in most database systems.




You started with a too complicated intention (changing lines, ...). First you must understand what a file IS.

Read a file and print it's content to the screen, print out the number of lines you've read, the length of each line. Remove leading and trailing space characters.

Try to play around with only a file writer, create some files, append to an existing file. Try to give all lines the same length.

Only after this experiments try to change a file.

To delete or rename files, use the File.rename() and File.delete() methods.
Btw: You can't delete or rename while accessing a file. You must first close all readers and writers.

shane lancaster

Posts: 1
Nickname: awsmosis
Registered: Sep, 2009

Re: Delete/edit/add to a text file or recreate? Posted: Sep 8, 2009 6:56 PM
Reply to this message Reply
@ Matthias Neumair

Thank you so much for this Matthias. I don't know if it helped anyone else, but it helped me out a ton!

Flat View: This topic has 4 replies on 1 page
Topic: EJB3 My own SequenceGenerator Previous Topic   Next Topic Topic: Chat room questions


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us