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.
Related
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.
I am using RandomAccessFile but writeBytes() overwrites the
existing content Mostly i want to implement this without using any new
temporary file at least methods name few clues or techniques will do.
To insert without the use of any temporary file, you'll have to find a way to read in and shift all subsequent lines down by one when you insert a new line.
The sequential storage of lines of text poses the same kind of issues that the use of a standard array (vs. a LinkedList) does: You can't just unhook a pointer, plop in some text, and then hook up the pointer to next item to point to a subsequent line in the file. You have to perform the entire shift.
So I'd guess that you'd want to go to end of file, shift it down by a line, then move up each line and perform the same shift until you hit the position at which you want to insert the new line, at which point, you'll have cleared a space for it.
This seems very performance inefficient.
Hopefully someone knows of a better way, but this would be my approach, were a temporary file not an option.
(Alternately, you could also always just read the whole thing into a StringBuffer if it were small enough, peform the insert within that, and then write the file back out from scratch, but I imagine that you've considered that option already.)
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
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).
I have a package with a GUI, and in this GUI I need to read a text file (call it list.txt) that may look like
8:00am something
9:00am somethingelse
1:00pm something different
With each time/event on a separate line.
How do I read each line separately and extract the time portion from each line? What I need to accomplish is to compare times in a list like this to a range of times I have previously determined, and reprint the list with only events/times in that range, but I'm not sure on how to read it line by line.
How does the question not make sense? All I am asking is how to take a textFile (one written in a separate java package) and how to basically read it one line at a time and take the time portion from each line. The time is always at the start of each line. I'm sorry, but I don't know how to be more clear on that, that is about all there is to it.
And The user provides the text file name via GUI application, as well as a time range. But that part I have down.
Use java.io.BufferedReader's readLine() method to read file line-by-line.
Use java.lang.String's indexOf() method to locate the first space.
Use java.lang.String's substring() method to extract before and after the space.