Java/ Writing in the first line, by out.write - java

I try to find a way to write in the first line of a file.
The problem is that I write to the file about 3000 lines (by out.write), but just in the end of this writing I have a information that needed to be insert to the first line of this file.
There is a easy way to do that?

There is a easy way to do that?
Nope. This isn't a matter of Java being awkward - it's just file systems, which don't generally allow you to insert data within a file.
Options:
Generate the file in memory (3000 lines isn't a lot) and then write it all out when you know the first line
Write out the file without the first line, then start a new file by writing out the first line, and then copying the "old" file line by line
If you know the length of the first line (in bytes) before you start, you could leave enough room, write out the length of the file, and then overwrite the first line

You can use two stream, one for your current and one for final.
When write end, write information first and then write your current stream to the final stream.
OutputStream finalOs;
OutputStream dataOs;
//...write your data
finalOs.write(your information);
finalOs.write(dataOs);// just 4 example

Related

Can I access a file with a specific index or line number that I want to access?

I am having an issue with reading a file in java. the issue is I have one method "sender", that calls another method "getMessage". when I call getMessage I need to read from a file. each line of the file is a message. I need to open the file, get the message and return it back to sender. I have it finding the file, opening it and getting the first line, but the issue is every time I return the first message and try to come back for the second message, it reads the first line again. is there a way to specify I want the second line? or I could even keep track of the length of each message so I know the index of what character I want to start with but I don't know how to access the file with an index.
it reads the first line again. is there a way to specify I want the second line?
No; not unless you remember the position (in bytes, not characters and certainly not lines!) to start reading on. Files are bags o bytes, not 'sets of lines', the file system just doesn't work that way. The only way to 'navigate to the 814095th line' is to start reading from the top until you have read through 814095 newline symbols.
To access a file at some index, generally you'd use a new RandomAccessFile, but I doubt you really want this - why not just keep the InputStream open, inside a long-lived object, that has a 'getNextMessage' method that pulls exactly 1 line off every time you call it. BufferedReader is pretty much exactly this - already exists in java. Just use that.

Java: edit/remove a specific line (with streams) directly into the original file

This is my scenario: I have a huge .txt file (~16GB) that contains some lines that have to be removed (that can easily be found with a .contains()).
Obviously the idea of loading the whole file on RAM and explore it with a Scanner, then saving the results in a new .txt file is unfeasible (since I have 16GB of RAM).
I also know that with the streams I'm able to read the file line by line, avoiding the memory leak. What I don't know (and haven't been able to find) is wether it is possible to edit that specific line and put it back in its place into the file.
Otherwise, is it possible to just rewrite a new .txt file with just the "correct" lines in a similar way to the one that allows me to read line by line, so without loading the whole file in memory, since before or after it will become as huge as the original one?
Set up a Stream Reader that will be reading your file line by line, and a Stream writer that will be outputting to the new file. If the reader doesn't find the line to edit the writer simply writes that line to the new file. If the reader spots the line it wishes to edit, create a method to manipulate the line and return it, then have the Writer write that manipulated line rather than the original one.
You won't be allowed to access the same file with a Streamreader and Streamwriter.

A Little complicated Java case - What is the fastest way to write calculations data onto a file

I am using BufferedReader (br) and BufferedWriter (bw) to read a very large file, do calculations, and write the results on the output file. The output file will have the same number of lines as in the input file.
What I am currently doing is getting data from the input file line by line (while br.readLine() != null), do the calculations of the line I just read, then write the result of this single calculation onto the output file (br.write) then write a new line (bw.newLine()). This loop repeats until it reaches the end of the file.
This works not bad.. but it takes me 1 second to process 3500 input lines. I have been told that it is two much as the code will be tested with MUCH LARGER files. What is the best practice I should use (both read and write)? Can I keep my results as chunks in the buffer until specific limit an then write to the actual file?
EDIT: I think the reading/performing calculation part is good, but how about writing by keeping parts in the buffer then writing to the output file? Is there a good way to avoid writing every iteration?

Overwrite part of text file in Java

I have a Java program that I need to output to a file.
This output file has a fixed structure, let's say a header, a START line, my output, an END line and a footer.
Everytime I run my program, I want it to write the output between those START-END parts. And if there's some text, I want to overwrite it.
By now, I'm reading line by line until I detect the START line, then I write my output. There's an "END" line after my output, as I said before.
My doubt is how can I overwrite the text between START and END (the older output) for every execution (the new output).
Are you familiar with RandomAccessFile class? I assume you have a variable length of body to be written between Header+START and END+Footer markers? This means you cannot just overwrite body part and expect tailing bytes to be pushed forward.
Maybe easiest implementation is how you started implement it anyway.
Open RandomAccessFile access
Find or skip to the end of START index, remember index
Read bytes from the end backward until found a start of END index, bytes were put to a tailBuffer while reading backward (is backward ordered due to a reversed read-write)
seek position back to STARTIndex+1 and write new body bytes to the end of start block
call raf.setLength(startLen+bodylen+endLen) to trim or extend a new file length accordingly
write tailBuffer to the end of file, make sure write is reversed in a proper order
This could be one way to implement this, or just read everything to RAM find indexes, overwite file with new content. This is fine if RAM is not an issue.

Reading File in Java and memorize bytes where it stops

i've to read in a .txt file. That File contains 13 parameters seperated by ",". I read it line by line, split after "," and wrote those 13 parameters in a database. But theres one Problem :
That file gets a bit bigger everyday (~ 2mb), so reading it by line will soon take a lot of time. So i thought of the following:
I want to read the file, then memorize the amount of bytes where the file finishes, write this "pointer" in a database and then next time start reading AFTER that bytes where the pointer is pointing to. (This way i don't have to read the whole stuff i already have again).
How can i do this?
Thanks!
You can do this using Random Access File. It lets you access file randomly, and thereby, you can start reading the file, from wherever you need to(not necessarily from the start).
According to this article: http://bitsofinfo.wordpress.com/2009/04/15/how-to-read-a-specific-line-from-a-very-large-file-in-java/, BufferedReader has a skip() method that could be used to jump into the file (in a seemingly similar way to Random Access File).

Categories