delete single line in a text file? - java

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 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 :)

Related

What is faster: rewrite file or delete and then create file use java?

Need update the file. I see two ways: first - rewrite file (merge content), second - delete previous file and then create new file with new content. I pass content for all file and its weight around 1 KB. What's the way faster?
There's no need to delete a file before you recreate it. You can just overwrite it with the new content. You can't really "merge" a file, because on the filesystem level blocks will be completely overwritten anyway, even if you just change 1 byte.
If you have the contents you want to end up in the file already, just overwrite the file.
If you have update data that depends on the existing file, read the file into memory (provided it's small enough), merge the data in memory and then overwrite the file. It's not that complicated.
Not sure practically but in theory merge content shd be faster. Delete n create means - remove pointer for the file, create new one and allocate memory and disk space for it. Merge means allocation is already there it just need expand it. While merging existing file content doesn't need to called in memory on the new one being merged, in creating new file whole lot will be in memory management. Unless there are lots of files OR it's going to make large file over time you won't notice it

Method to copy to a pre-existing file without declaring a new file?

Essentially I have an application that needs to pull a .txt file from an S3 bucket and write it's contents into a local text file so that another class can pull the data and update the associated jFrame.
Before this snippet of code I have been deleting after use, however if I need to refresh the jFrame a preexistingfileexception as expected. The whole limiting factor is the fact that to manipulate "input.txt" I need to re-declare it as a file in each statement.
I have tried using filewriter and many other copy methods but end up with the same issue everytime. Trying to delete to the path the file is located, circumventing the need declare the file name leads to a file in use exception. Ultimately this will be pushed to a web application and caching will solve all of my issues but for now I just would really like to have the local application running properly for demonstration.
Really anyway to copy to a file or manipulate a file without new File("input.txt") would be great.
s3object.getObjectMetadata().getContentType());
InputStream input = s3object.getObjectContent();
Files.copy(input, new File("input.txt").toPath());

JAVA - Read only new lines in a file

I am writing a code in Java to read lines from a file. It is required by the problem statement that the code reads the same file multiple times. However, it should only read new lines, without using any flag of any sort. Please suggest ways on how I can approach this. Any ideas are welcome.
There is no way to "only read new lines." To achieve what you're looking to do, I would suggest caching the old version of the file and comparing the new file every time you re-read it with the old cached one. You will be able to detect the new lines and any other change in the file. After you are done analyzing, overwrite the old cache saving the newest read.

How to delete a record and keep on reading a file?

I have to read a sequential file which has over a million of records. I have to read each line/record and have to delete that record/line from the file and keep on reading.
Not finding any example on how to do that without using temporary file or creating/recreating a new file of the same name.
These are text files. Each file is about .5 GB big and we have over a million lines/records in each file.
Currently we are copying all the records to memory as we do not want to re-process any record if any thing happens in the middle of the processing of a file.
Assuming that the file in question is a simple sequential file - you can't. In the Java file model, deleting part of a file implies deleting all of it after the deletion point.
Some alternative approaches are:
In your process copy the file, omitting the parts you want deleted. This is the normal way of doing this.
Overwrite the parts of the file you want deleted with some value that you know never occurs in the file, and then at a later date copy the file, removing the marked parts.
Store the entire file in memory, edit it as required, and write it again. Just because you have a million records doesn't make that impossible. If your files are 0.5GB, as you say, then this approach is almost certainly viable.
Each time you delete some record, copy all of the contents of the file after the deletion to its new position. This will be incredibly inefficient and error-prone.
Unless you can store the file in memory, using a temporary file is the most efficient. That's why everyone does it.
If this is some kind of database, then that's an entirely different question.
EDIT: Since I answered this. comments have indicated that what the user wants to do is use deletion to keep track of which records have already been processed. If that is the case, there are much simpler ways of doing this. One good way is to write a file which just contains a count of how many bytes (or records) of the file have been processed. If the processor crashes, update the file by deleting the records that have been processed and start again.
Files are unstructured streams of bytes; there is no record structure. You can not "delete" a "line" from an unstructured stream of bytes.
The basic algorithm you need to use is this:
create temporary file.
open input file
if at the end of the file, goto line 7
read a line from the input file
if the line is not to be deleted, write it to the temporary file
goto line 3
close the input file.
close the temporary file.
delete (or just rename) the input file.
rename (or move) the temporary file to have the original name of the input file.
There is a similar question asked, "Java - Find a line in a file and remove".
Basically they all use a temp file, there is no harm doing so. So why not just do it? It will not affect your performance much and can avoid some errors.
Why not a simple sed -si '/line I want to delete/d' big_file?

How to Update Code of a Serialized file in Java

I've made an update to my game in which the save file has been updated, including an array. The problem I have is that the old save file doesn't have this array, so when the code tries to access it it gets an outofbounds error. The obvious solution would be to delete the save file and let the game recreate it, but that would result in the deletion of highscores. I have added a method in the save class but it seems to have no effect, as the original didn't have it.
Is there any way of updating the save file without deleting the data within? Another option might be to create another save file, but it would be tedious to create a new one for every update.
Any help or advice would be greatly appreciated.
Thinking loudly maybe read data from file to the memory, clear data inside file, add missing array to previous file's content and write stream again to the same file?

Categories

Resources