954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

editing file

Hi,I need to edit in a file without deleting all the data.
Thanks

nouryn
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

As I do not know which exactly writer you using only thing I can say is to look for append() method which will be either directly implemented in used writer class or inherited from other classes above this one

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
Hi,I need to edit in a file without deleting all the data. Thanks

do you need to edit the data in a file, append data, delete parts of the file (without deleting ALL of the data, as you put it), ...

anyway, open notepad, type something, close and save it.
re-open the file, add a line, press 'ctrl+s' and your job is done.

don't need to thank me, the bill 'll come later :P

if your task is to create an application that does such, start writing code, begin with the easy parts, and come back here when you get stuck.

when you do so, post the relevant code, post what it should do, post what it is doing, post any error messages and whatever you think WE think might be relevant

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

first i know how to open a file and write in it and save it I'm not gonna waste my time for such a thing.second my problem is i write to the file customer data and then the customer can edit it so i want to edit this data in the file (adding the new one and deleting the old data)I opened a temp file but it's not working can i do it without temp files?how?.
Thanks

nouryn
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
first i know how to open a file and write in it and save it I'm not gonna waste my time for such a thing.second my problem is i write to the file customer data and then the customer can edit it so i want to edit this data in the file (adding the new one and deleting the old data)I opened a temp file but it's not working can i do it without temp files?how?. Thanks

when you want to change data in a txt file (manually) do you first create a temp file? no, so why would this be different?
just overwrite what you want to change

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

@nouryn - if you can provide better description of user editing process we may able to help. However right now as you can see from the responses it is not clear what you try to do. Some supporting coding would be welcome...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Are you familiar with the 'append' argument of the FileWriter ?

BufferedWriter writer= new BufferedWriter(new FileWriter(file, true));


When you try to write to the file, the new data will be appendedafter the existing

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

hi,sorry that i didn't describe my program clear enough.
i have a file contain the following
123 Nouryn current =5000
124 Jasmine investment =300

the customer want to add to his account 300 so i search for his name and then search for ("=")so he take the credit and store it so the customer can add on it.
the user want to add 300 so the new credit will be 5300 this data must be written in the file but after deleting the old one how to do it.

now i;m trying to put the new data and the old ones in a new file but only the data after the edited line is written:s.i've attach my code hope i explained well this time.
help plllllllllllllllzzzzzzzzzzzzzz

Attachments DeleteFile2.java (1.27KB) SearchFile.java (4.33KB) OldCustomer.java (4.19KB)
nouryn
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

A simple way to do it is read the entire file and store its data somewhere (for example a Vector). Then change the credit in the Vector, after you find the right element, and save the Vector with the new data back to the file

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

sorry but i don't know how to use vectors.can i do it with a different way??
Thanks

nouryn
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

Well, now is a good time to learn about them (actually Lists): http://java.sun.com/docs/books/tutorial/collections/index.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
sorry but i don't know how to use vectors.can i do it with a different way?? Thanks

yes, you can use banana-splits. if you can't figure out how to use a Vector, don't bother with how to write/edit files

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

You can use patterns. Open the file with the data and store lines one by one to a string:

String str;
while (( str = br.readLine()) != null) {
// your code here
}

Inside the while loop, you can use patterns/matchers to see if the current line (str) has the name you are looking for. If it does, you can change the value after equal sign to whatever you want to. Then, add str to your new file.

If you want, after while loop you can delete the old file.

Here, a way without using vectors. Hope it helps...

etdm
Newbie Poster
1 post since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

So you guys are suggesting reading the entire file into Objects and then editing and writing it back out? Seems inefficient to me - there's no better way? I'm sure there's a way to just have a carat position and then write things there. Not saying your way isn't good, just curious.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Yes there are better ways of editing data at a random position; look into RandomAccessFile, though things can get complicated pretty soon.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

You can use MappedByteBuffers as well, but as s.o.s. mentioned, they are more complex.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You