Reading File in Java and memorize bytes where it stops - java

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

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.

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.

Delete file contents using RandomAccessFile

I have a file which contains lot of zeros and as per the requirement the zeros in the file are invalid. I am using RandomAccessFile api to locate data in the file. Is there way so that all the zeros can be removed from the file using the same api.
You'll have to stream through the file and write out the content, minus the zeros, to a separate temporary file. You can then close and delete the original and rename the new file to the old file name. That's your best alternative for this particular use case.
You can use RandomAccessFile to read the files' data, and when you reach a point where you need to change the data you can overwrite the existing number of bytes with equal number of bytes. It's iff the new value is exactly the same length as the old value.
With RandomAccessFile its difficult and equally complex when the size of two, the one being changed and the new value are different. It involves a lot of seeks, reads and writes to move data back
Try to read the whole file, change the bits you have to change and write a new file. You might process one line at a time or read the whole file into memory, modify it and write it all back out again. It is a good idea to perform the edit in the following manner:
Read file
Write to Temporary File [just to back-up]
Rename original to back-up
Work on Temporary file.
Remove Backup if you were successful.

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

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

How to read read the contents of a text file in a separate Java package in a 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.

Categories