Hi forum,
I' ve a big text file with a lot of line. I want read only last line or last x lines without read all the lines.
This file store date and room temperature every 10 minutes and add a line each time. I want display a graph of some hours only, but problem is that I don't know how many lines there are in this file and I implemented a cicle to read all the line and use last 144 (for show 24h) only.
This take a lot of time.
There is a fastest way?
If you are comfortable with SQLite, it is the way to go instead a large text file. You can import the text file into a table.
Suppose you have a table named: tblTemp with 2 cols: TEMP_DATE And TEMP, you can sort the table by any column in any direction and extract any number of records. Example to extract the most recent 144 records:
B4X:
Dim strQuery As String="SELECT * FROM tblTemp ORDER BY TEMP_DATE DESC LIMIT 144"
cursor=Starter.sql1.ExecQuery(strQuery)
If your not up with SQLite .. you could experiment with File.ReadList method .. writing your text file to a list first for easier interaction.
this is only basic hint code .. unsure how and what your writing to the text file. * You would have to include some checks for list size etc.
B4X:
Dim List1 As List
List1 = File.ReadList(File.DirDefaultExternal, "Text.txt")
Log($"List size = ${List1.Size}"$)
Log($"This is the last item ... ${List1.Get(List1.Size -1}"$))
For i = List1.Size - 144 To List1.Size -1
Log($"This is item # ${List1.Get(i}"$))
Next
But Personally I would be following @Mahares suggestion above and go the SQLite Database approach.
I don't know database and SQL. I used access on vb.net only with simple interaction. This is why I store in a text file.
But database is the best solution. I know...
I will try the second solution
Shell commands can cause all kinds of problems including permission problems, missing commands and others. It will also be slower than reading the file directly from the app.
This surprised me. I would have imagined that tail would be a lot faster considering it's specialized for the job of getting lines from the end, especially for very large files.
Remember that the OS needs to start a new process for this. An efficient implementation inside your app will be faster. Though this is not a real issue if you are reading the file every 10 minutes.
Android <> Linux. Accessing the underlying shell commands is not something that should normally be done.
just save the last 144 rows (for the last 24h) in a separated file from your .net app.
u can use the same file name or for each day the date.
in .net you can store the data in a list.
just save the last 144 rows (for the last 24h) in a separated file from your .net app.
u can use the same file name or for each day the date.
in .net you can store the data in a list.
Another way could be to open the file and skip the first "x" records.
On the assumption that each row is of equal length, you could adapt the attached code where a couple of alternatives are showed. You should time each one against your real data file in order to understand which one is faster and less resource consuming. Along with the already suggested solutions.
Happy new year!
@Erel : I think that the addition of the "skip" method to the standard File I/O methods could be a nice one; I'm thinking about chunk-reading operations..
Hi Erel, I must admit that at first I didn't fully understand your comment.
Yesterday I wrote a quick and dirty hexeditor in B4J and the light shed on me again.
So, InputStream is OK for one-way only reading (and skip could be of some help like in the OP), while RAF is a b-oriented, rich featurs mechanism although "heavier" than the former (FileChannel?).
So, InputStream is OK for one-way only reading (and skip could be of some help like in the OP), while RAF is a b-oriented, rich featurs mechanism although "heavier" than the former (FileChannel?).