inserting data in the middle of a text file through java - java

I want to insert the data at some positions in the text file without actually overwriting on the existing data.I tried RandomAccessFile ....but that also overwrites it....
Is there any way to insert the data without overwriting??
-Thanks in advance

You have to read your file and rewrite it. During this operation you have to find the place where you want to put your text and write it.

It is not possible (not with Java, and not with any other programming language) to "just" insert data in the middle of the file, without having to re-write the rest of the file. The problem is not the programming language, the problem is the way files work.

Related

Best file type to save budget data in Java?

I'm starting on a new budgeting/allowance project and I'm trying to figure out how to save the data between opening and closing the program. Arrays seem clunky and I don't know how I would save the array data to a file anyway. I've really only worked with text files before so this is new to me.
I'm assuming a database of some sort is what I need but I don't know what I should be looking for.
I know this has to be a simple issue but I honestly don't know where to start; any help is greatly appreciated.
That is entirely for you to decide, there is no one right answer here.
You can save in a text file, e.g. CSV if very simple, otherwise JSON or XML are common choices, or in a binary file, e.g. Java serialized objects, or some embedded database file might do.
It really depends on how complex the data is, how big the file can become, whether you want to be able to edit the file directly in a text editor, and how important load/save performance is to you.
Since it's a new project and you seem fairly new to this, I'd suggest JSON or XML, whichever of the two you are more familiar with. But that's just my opinion.
It's entirely your choice.

Storing, retrieveing, editing and deleting data

I am coding a questionbank with netbeans as a school project. I am using a JFrame GUI to enter question data. I need some clever way to store, edit and delete data in some file. I donĀ“t want to use any databases. I already tried solving this with txt files but it is tedious and wont work. I need some option where I can add an entire question to the file and then when I retrieve it retrieve certain parts. The data below is how I tried solving it. I then used a scanner to read the files line by line and stop everytime a "_" is read. Is there any better way to store this. Or can i store this in a 2 dimensional array in the java program itself.If so any help or solution is appreciated
Geography_What is England's capital_Berlin_Manchester_Dover_London_D_3
Maths_What is 2+3_7_9_5_6_C_1
Economics_What is demand_idk_stuff_demand_supply_C_2
Well, if you dont want databases or any external files, the only way to "store" information is to create some data structures to do so. The issue with that is they wont persist past the lifetime of the current running application.
Id advise to use some form of external file, or set up a simple sql db. Its easy and will make the project simpler

How to replace a specific row from a CSV file?

Is it possible to access a specific row in a CSV file and replace a specific field in it? E.g. like in this pseudo code:
Row row = csvFile.getRow(123);
row.getField(2).set("someString");
Tried Apache Commons CSV and OpenCSV, but can't figure out how to do it with them. Only way I can think of is to iterate through a file, store everything in a new object and create a file, but that doesn't seem like a good way.
Thanks for any suggestion!
Sounds like a duplicate of this one to me.
I am not sure about how to do it with apache but I am sure they have the same mechanisms. In openCSV you would create an CSVReader to your source file and a CSVWriter to your destination file. Then read a record at a time, make any desired changes and write it out.
Or, if the file is small, you can do a readAll and writeAll.
But if you want to do it all in the same file read my comments in the duplicate.

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?

Text editor in J2ME - Store text in memory to edit

I'm developing a text editor in J2ME for editing source code, and because it has special features like syntax coloring, I can't use the regular TextBox, so I have to make a text box from scratch, using Canvas.
I found the way of reading/writing files from/to memory card, using FileConnection and the InputStreamReader/OutputStreamWriter classes for read and write text.
Now the problem is, when I read the file, how I can store the read information in memory, in order to edit the text freely and decide later if I can save or discard the changes?
Do I create a temporary file where I store the data for editing? But how can I write/delete text in middle of the file? Or do I have to dump the data in a StringBuffer?
Any methods or alternatives will be welcome.
Thanks!
I'd just use String (for storing the whole text in one variable)
or Vector of Strings (for storing the text line by line).
Temporary files is a very bad solution.

Categories

Resources