android specific writing in File - java

I want to have an internal File like stock features with these properties:
File has a Maximum of LineNumber i.e.: 400.
New line appends to file until this limitation is reached; then, firstly removes the First Line and then appends New Line.
To remove a line it shouldn't be necessary to read and rewrite the whole file.
Is it possible to do it so?
Many thanks and best regards

You need a circular file buffer. There is nothing built into Android, but you can use This implementation which I have used in the past and works well for me.

Related

reading a file from last line

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.

How to etermine if a file is appended or outright changed

I'm wondering the best method for my program to determine if a file has been appended or completely changed.
What I am currently doing is using a LogIOThread I wrote that is using a FileInputStream.avail to check if a file has been appended. If so, I am appending to a non-editable JTextArea (actually a modified JTextArea with variable scroll features). My end-game was to have a autoscrolling JTextArea that is basically scrolling a log4j outputted logfile. This is working great for me right now. I'd like to adapt this to a non-log files.
What I would like to add to the LogIOThread is a monitor that will check if the file contents changed rather than just have new appended text. The first use case I am trying to solve is the file gets rewritten mid run. At the most basic level I figured I could use this function to reload my JTextArea close and reopen the FileInputStream, and start over if the file get overwritten.
I figured while I was doing that I might want to make it more robust to handle the second use case of mid-file insertions, like a properties file change. I figured I could get the textArea to do a line replace if I can figure out if a line changed.
I have a requirement (not set by me) to use Java6, so some of the new nio FileWatcher's are no good for me. Notifiers in general seem counter productive as I'm catching appends via FileInputStream. This also led me to discount other libs such as Commons and jnotify. I could be wrong though, and maybe FileInputStream.avail is not the best way for me anymore.
My first thought was that I could store the file size, and check if it drastically changed (to a value less than stored). Since a new created log file would need a fresh textArea, this doesn't seem to bad, and solves my first use case. This would not work for the second use case of a specific value change in the file. It also might be problematic for a file that gets recreated with a consistent size while my IOThread is sleeping, although I doubt it's likely.
My second thought was that I could continually check the file modified time, and if there is no appendable text, I would reread the file and do a line comparison? I would need to do this anyway if I'm going to change a line in the textArea unless I reload it every time. This seems horribly inefficient though.
Any suggestions? I'm not opposed to changing how the LogIOThread works if there is a suggestion to get new text and changes with something better than an avail + file modification check combo.
If the file size decreases it has certainly been overwritten. However it may also be overwritten with something as large or larger, so the converse does not hold.
Don't use available() as a surrogate for File.length(). See the Javadoc. That's not what it's for.
How about creating an intermediate OutputStream that reads the log data, makes it available to the JTextField and then routes that data to the file?

How to append existing line within a java text file

I'm having trouble adding to an exsisting line in a text file without overwriting that particular line or adding a new line.
for example, i have a line in my text file which is:
hello my name is
I would like to add to this line so it becomes:
hello my name is joe bloggs
Thanks
i have a task to create a help desk program and i am trying to incorporate a feature that enables users to edit questions they have posted. as a result, the program will need to be able to append Any line within the text file - not necessarily just the last line
If it's not at the end of the file, you're in trouble - you're basically talking about inserting data in the middle of a file, which isn't traditionally supported by file systems.
The normal way to approach this is to create a new file - copy the portion before the insertion point from the old file, then write your new data, then copy the remainder of the original file afterwards. Finally, do whatever renaming/deleting you need.

Partial reading of file in Java

I am creating a Java application in which I need to read the first few lines of a huge text file and do the processing.
Is it possible that instead of getting the entire file, I read the first few lines and fetch the data? And this is being done using Java API.
Use BufferedReader.
Yes, it can be done. When you use BufferedReader, for example, you read just (buffer_size) from the file. Then you can process it before reading the next fragment...
for example, see this tutorial
There is also LineNumberReader if you need to keep track of the line numbers

In java, how do i edit 1 line of a text file?

Ok so I know the value of the line, I dont have the line number, how would I edit only 1 line?
Its a config file, i.e
x=y
I want a command to edit x=y to x=y,z.
or even x=z.
In Java you can use `Properties class:
app.config file:
x=y
java:
public void writeConfig() throws Exception {
Properties tempProp = new Properties();
tempProp.load(new FileInputStream("app.config"));
tempProp.setProperty("x", "y,z");
tempProp.store(new FileOutputStream("app.config"), null);
}
If you are using that configuration format, you might want to use
java.util.Properties
component to read/write on that file.
But if you just want to edit it by hand, you can just read the file line by line and match the variable you want to change.
One way to do it is to:
Read the file into memory; e.g. as an array of Strings representing the lines of the file.
Locate the String/line you want to change.
Use a regex (or whatever) to modify the String/line
Write a new version of the file from the in memory version.
There are many variations on this. You also need to take care when you write the new version of the file to guard against losing everything if something goes wrong during the write. (Typically you write the new version to a temporary file, rename the old version out of the way (e.g. as a backup) and rename the new version in place of the old one.)
Unfortunately, there is no way to add or remove characters in the middle of a regular text file without rewriting a large part of the file. This "problem" is not specific to Java. It is fundamental to the way that text files are modelled / represented on most mainstream operating systems.
Unless the new line has the exact same length as the old one, your best bet is to
Open a temporary output file
Read the config file, line by line
Search for your key
If you can't find it, just write the line you just read to the output file
If you can find it, write the new value to the temporary file instead
Until you hit EOF
Delete old file
Rename new file to the old file
IF your config file is small, you can also do the whole parsing/modification step in memory and then write the final result back to the config file, that way you skip the temporary file (although a temporary file is a good way to prevent corruption if something breaks while you write the file).
If this is not what you're looking for, you should edit your question to be a lot more clear. I'm just guessing what you're asking for.
If your data is all key and value pairs, for example ...
key1=value1
key2=value2
... then load them into a Properties object. Off the top of my head, you'll need a FileInputStream to load the properties, modify with myProperties.put(key, value) and then save the properties with the use of a FileOutputStream.
Hope this helps!
rh

Categories