I'm currently trying to make a very fast application that can write data to a file very quickly. However, the only way to do this is if I'm able to access a specific offset inside a file and write a boolean there.
First of all, I'm assuming that ObjectOutputStream(FileOutputStream) always writes data to the file the same way, keeping the same structure for the data.
Secondly, what I need to do is to access a specific offset of a boolean inside a boolean[][] inside the file. Is there any Java class that "scrolls" through the data structure created inside a file by OOS?
If not, maybe someone can shed some light on how to do this very fast or on how to do this ensuring the file does not get corrupted (hence the data not getting corrupted).
Related
I'm trying to implement a file storage system that is like a video recording system that loops over existing data. Say, we have a maximum file size of 10MB and append an integer value every second. We can setup a FileChannel and keep appending the values. But what to do once we've reached our 10MB? Now we want to append a new value but pop one off the head of the file. We see the problem is a sort of queue and it's easy to put and pop values from a queue in memory, but not so easy when using files.
I implemented a circular buffer with a FileChannel as the behind the scenes storage. It works but the problem is the first and last indices move through the file as data is added and removed. Ideally, I would always like the oldest data value to be at file index 0 and the most recent data at file index n-1, so that when a file is read it is from the start to end.
I saw that FileChannel supports methods transferTo() and transferFrom() and also did an implementation using these methods and again it works. The problem with this method is continually having to transfer blocks of data from the current file to a temporary file and then replace the current with the new file. It works but not particularly efficient.
Thus, I've tried a few things but not found the ideal solution or replicating a file-queue as yet and was wondering if anyone else has implemented the golden bullet solution? Maybe, a file version of a queue in which the data is shuffled along is simply not possible, but hopefully someone knows an answer. Thanks.
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());
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?
I am trying to download a file from a server in a user specified number of parts (n). So there is a file of x bytes divided into n parts with each part downloading a piece of the whole file at the same time. I am using threads to implement this, but I have not worked with http before and do not really understand how downloading a file really works. I have read up on it and it seems "Range" needs to be used, but I do not know how to download different parts and being able to append them without corrupting the data.
(Since it's a homework assignment I will only give you a hint)
Appending to a single file will not help you at all, since this will mess up the data. You have two alternatives:
Download from each thread to a separate temporary file and then merge the temporary files in the right order to create the final file. This is probably easier to conceive, but a rather ugly and inefficient approach.
Do not stick to the usual stream-style semantics - use random access (1, 2) to write data from each thread straight to the right location within the output 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 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 :)