开发者

delete single line in a text file?

I have created j2me application for read write of text file

now at time of reading I read one line and send it to server. after that I want to remove that开发者_JS百科 line from text file.

I am not getting how to do it. in some example I found solution as copy original file content in one object then remove that string from object and then delete original file and create new with that new object.

I don't think it as good approach. is there any other way to do so???

Edit:

actually problem is like one application is writing some data in text file and my another application read one line send to server and remove that line.

Now if I go with the approach like copy new object and delete file and write new file with new object then I will found one problem

if file is deleted then first application can't found that file so it may create new file with only one data and second application will create new file based on new object so my data will be lost

Edit:

Even I tried to do same thing with RMS but when both application is accessing same RMS at that time all data in RMS file are clear. First application open RMS for writing and second Open for sync and delete. but at time when both are opening RMS all data clear.

Is it possible to set lock on RMS file from one application??


No, that's how you do it.

You can't delete a line from the beginning of a file. You would need to re-write the file without that line.

(Note that this is not specific to java)


As records are inserted i was creating single file for single record in one specific folder now as that file is read by background application and send to server that will be deleted by application.

so it solve concurrency problem in file read write.

i know it is not good approach but i didn't find any other good approach.


Most file systems don't have a mechanism for deleting stuff in the middle. (pretty sure that's the case in j2me). So a standard practice is open a new file; copy the old file up to the point where the unwanted line goes, skip it, then copy the rest of the file. I know it sounds inelegant but that's just how it is :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜