I would like to read a file from last line using RandomAccessFile. Is this possible or do I have to use another class?
Beside this file changes during the time so the last line doesn't remain last forever. During the reading another, java program write on it. My question is: the program will see in the same time another java program write on the file, the changes?
Edit
Well suppose I have a server that write its faults in a error log file during it's running.another program reads every line.which should be the best way?
Yes reading a file from the bottom up is possible using RandomAccessFile:
Reading the Last Line of a File in Java through Random Access
as for the other part of your question:
Beside this file changes during the time so the last line doesn't
remain last forever.During the reading another java program write on
it.My question is: the program will see in the same time another java
program write on the file, the changes?
I would propose a SSCCE in which you show what you are trying to accomplish and the problem
EDIT:
As Jon Skeets comment suggests, I found a link to a similar question answered by him: Quickly read the last line of a text file?
EDIT 2:
I think I got your second question, I'm not sure it's possible, as a single file cant be accessed by 2 different streams at the same time, one will just throw an error when trying to open the file. Ypu can however monitor if changes occur after the file has been read using Java.NIO Directory Watcher, Unless I misunderstood you.
Related
So my assignment is to read a csv file containing 'employee' objects. This file contains errors within it, for example a normal csv line should read:
EmployeeId,FirstName,LastName,DOB,Gender,Position
Some lines of the file however are either in the wrong format or contain null values. E.g:
9658273,bad
I need to be able to read the file and use exceptions to identify where the errors occur and write these 'bad lines' to a new file called bad.txt.
I am unsure where to start at this point and need some direction. I know how to read a file that has no errors but I'm having trouble writing exceptions.
So the first thing you should look at is what constitutes a 'bad line'.
Write up some if statements for this:
For example (this is just logic/pseudocode):
if i have a null line:
write this line into the error file
else if i have a line with first name before employeeid:
write this line into the error file
etc, etc
You will need to figure out how to tell when a entry is out of order, which will probably be looking at types. For instance, name should not be a date/time. While Gender should not be an ID of 1028473AA848737.
Don't worry too much about writing the error into error.txt for now. Start with a simple:
if i detect an error:
print out "ERROR DETECTED"
then once you have a case where you can see you are detecting the errors correctly, you can move onto writing them to a file.
Hope this might give you something to start off! Let me know if you need further guidance :)
You can see: https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ to read data, then validate them
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 want to read a file incrementally in java while the file is being modified/written by some other process. So suppose Process "A" is writing/logging a file "X" and another process "B" wants to incrementally read the file "X", say every 1 sec (or even continuously) to find a particular pattern. What's the best way to do this in java? I know I can use RandomAccessFile's 'seek' method but will that interfere with the writing of the file? Is there a better way to do this?
Poll the data modified of the file. Opening it up for reading can prevent other programs from writing to the file at the same time.
If you're able to use Java 7, you could take advantage of the WatchService ... but it doesn't solve having to parse the whole file.
The only thing I can think off is maintaining some kind of "marker" that would indicate the last position you were up to. The next time you came to read the file, you could skip to this point and read from there (updating the marker when you're done)
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.
Now I'm making a little program in Java which must read a really big file. Due to this thing, I want to access to the file but not read completely each time, then my question is the following: can I change the offset of the file descriptor with a simple instruction or the only solution that I have is read all the previous lines which I don't need?
In other words, can I simulate the lseek command in my input file?
I think it's not necessary this time, but if someone wants code, I'll post it.
Regards!
I think you probably want RandomAccessFile.
Specifically, you want the seek(long) method.