Hi!

How can i remove from a file a spesific line which includes "abc" string by using RandomAccessFile libary?

Thanks

Recommended Answers

All 5 Replies

You're going to have to read and re-write all the file starting at the "abc" line and moving everything down by the length of the line. Not a pretty process. If the file's small enough to fit into memory you can simplify the code by reading it all into an array, deleting the abc line from the array, then re-writing the whole file from the array.

Oh God! rewriting the file ?!

@JamesCherrill What do you suggest? How to remove quickly a part of a file?

@JamesCherrill What do you suggest? How to remove quickly a part of a file?

That was my best suggestion, I don't know any other strategy.

Think of a file as a long strip of paper with designs on it. If you want to remove a design, you have to make a cut in the paper just after the design and drag the end piece over the design you want to remove.

Oh God! rewriting the file ?!

@JamesCherrill What do you suggest? How to remove quickly a part of a file?

Its really not that much coding...check here:http://www.daniweb.com/software-development/java/threads/188879 and here:http://www.daniweb.com/software-development/java/code/408638(shows logic of using stringbuffer replace()) and this:http://www.daniweb.com/software-development/java/threads/188879. I guess there are many ways, easiest though might be just to read in entire file into stringbuffer, find appropriate text using the knowledge from links.. and then use replace() method to change just that string and then write the stringbuffer back to file using random access file ofcourse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.