File observer using java - java

I am using watch Service in order to track changes made to a file and print these changes in a text area. suppose only new data will be written(no delete and no replace) to this file is there a way to get this new data without the need to rewrite the over all file when changes done.

The Watch Service isn't able to tell what changed about a file. Assuming you have access to the old content of the file, you could use a diff library like java-diff-utils to find the changes. Example 1 on their wiki should match up with your issue.
Check out

Related

How to change specific part of a file using java?

I was writing a program that implements a dictionary.
Actually what I did is just to write a java applet to show the words which is defined in a .xml file. And I did that with the org.w3c.dom package.
Now, I want to add a new feature that users can modify a word in the dictionary in the the program then the modification will be saved to the original .xml file.
Here is my question: what should I do to save the changes? Note that users can only modify one word a time so I don't want to load the whole file and modify the certain part and re-write the whole file to the disk. Is there a novel way to do that?
An XML file is a sequential text file. This means that there is no formula or other convenient way to locate the n-th word in a dictionary stored in XML. Elements need to be written one after the other, character by character (and one character may or may not result in a byte). Thus, what is called a random update, is out.
Look at JAXB for a most convenient way to read and write XML, and invest some work so that a user cannot update in memory and terminate the program without saving.
Reading and writing files in specific formats is a little bit trickier that what you portray.
Seen with "XML eyes" you are only changing a portion of the file - but to do that on the file level you need to seek to the position of change and write new bytes from there. The problem with that is that the content after that position won't adjust according to the new portion you write.
TL;DR - no - you need to read+write the complete XML file when making changes.

Update objects written to a text files in java

Writing Java objects or a List into a text file is ok. But I want to know how I can update or rewrite a object which was written previously without writing objects again. For example, let s assume there is a java.util.List has a set of Objects. and then that list is written to a text file. Then later that file will be read again and get all objects from list and then change one object's value at run time by a java application. Then I don't need to write entire list back to the text file. Instead only the updated object in the list is required to be rewritten or updated in the text file without rewriting the whole list again. Any suggestion, or helpful source with sample codes please.
Take a look at RandomAccessFile. This will let you seek to the place in the file you want, and only update the part that you want to update.
Also take a look at this question on stackoverflow.
Without some fairly complex logic, you won't usually be able to update an object without rewriting the entire file. For example, if one of the objects on your list contains a string "shortstring", and you need to update it with string "muchmuchlongerstring", there will be no space in the file for the longer string without rewriting all the following content in the file.
If you want to persist large object trees to a file and still have the ability to update them, your code will be less buggy and life will be simplified by using one of the many file-based DBs out there, like:
SQLite (see Java and SQLite)
Derby
H2 (disk-based tables)

Organizing data in java (on a mac)

I'm writing a tool to analyze stock market data. For this I download data and then save all the data corresponding to a stock as a double[][] 20*100000 array in a data.bin on my hd, I know I should put it in some database but this is simply performance wise the best method.
Now here is my problem: I need to do updates and search on the data:
Updates: I have to append new data to the end of the array as time progresses.
Search: I want to iterate over different data files to find a minimum or calculate moving averages etc.
I could do both of them by reading the whole file in and update it writing or do search in a specific area... but this is somewhat overkill since I don't need the whole data.
So my question is: Is there a library (in Java) or something similar to open/read/change parts of the binary file without having to open the whole file? Or searching through the file starting at a specific point?
RandomAccessFile allows seeking into particular position in a file and updating parts of the file or adding new data to the end without rewriting everything. See the tutorial here: http://docs.oracle.com/javase/tutorial/essential/io/rafs.html
You could try looking at Random Access Files:
Tutorial: http://docs.oracle.com/javase/tutorial/essential/io/rafs.html
API: http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html
... but you will still need to figure out the exact positions you want to read in a binary file.
You might want to consider moving to a database, maybe a small embedded one like H2 (http://www.h2database.com)

How do I create a .properties file from JSP?

I want to make a .properties file to use instead of numerous cookies. I have been trying to figure out how to do this by googleing, but maybe I'm just missing something or not looking for the right thing.
This will eventually be user based, so I wanted to know if I could get it to create a new file based on the user's login, but every beginners guide I find seems to say just make a text file by hand.
Could someone provide me with a page that explains how to do this well, or give me the basic code that I'll need to get me started.
And also what code I need to add new key,value's to the file and how to get the values from it.
Take a look at Properties.list. Using this method you can easily write properties into an OutputStream and with help of ByteArrayOutputStream you can convert theese properties into a string.
To read properties from a file use Properties.load.
Unfortunately there's no standard Java API to just add or remove a property from a file - you'll have first to load the whole file into a Properties instance and then save it back into the file after you made the required changes using Properties.setProperty.
java.util.Properties has load and store methods that will read and write the properties for you. In that case you just add to the Properties map and then store it to an OutputStream or a Writer.
For doing it user based, you would need a directory to store it in and then base the file name on the user name or id, but make sure it is safe for a file name.

Any existing java library to delete , edit , add certain line in a file with swing in Jmenu?

I am now using Fileutils to access a file to retrieve lines of phone number.
But now I need to add phone number , delete phone number and edit phone number in the file. I do know how to do it with JMenu , but i keep wondering , do anyone create a library for this?
I would suggest you to go for DB .
FILE IO in such case is very ugly coding.
and then also if you want to do it.
to modify content you need to create other file read from older and modify it in memory and write it back to new file.
If you don't want to go for DB, which is the best idea, you should maybe.
Read the whole file and store in a Collection
then, you remove the phone number, and then write it again on file overwriting.
It's definitely much more work. But it works too.
Reminding that the class of the ObjectType needs to implements Serializible.

Categories