I'm using java to parse XML files which come from a FTP protocol. The problem is, the file I take may being copied/modified by the FTP. So I need a method which can check whether the file is completely written.
I've tried using File::canWrite method (which did work at all) or finding the ending tag of the XML file but none of them works correctly at any case. The File::renameTo is pretty slow and doesn't look decent although it works (not all the case either). Is there any good and fast way to check a file if it's completely copied?
Thanks alot!
Short answer no. The best practice is to write to a file with a temporary name, for example somefile.part and rename it when done. The writing program needs to do that. The workaround when you don't control the writing application is to check the modification time and ensure that some reasonable time has passed since the most recent change. Perhaps a minute. Then you assume that the file is complete.
Related
I have a large directory containing files that are modified by a seperate system at varying intervals. I am running a watcher on this directory to detect which files are modified.
I'm wondering if there is some sort of trigger that occurs when a file is accessed by the system for modification. If so, the following would apply:
Using Java, is it possible to detect which files are about to be modified and make a temporary backup before that happens?
Alternately, is it possible to compare the newly modified file against it's previous version?
In this scenario, it is impossible to make a back up of every file as the files are large and there are many of them.
Example:
I have four files:
a.xml
b.xml
c.xml
d.log
b.xml has a new section added.
Is it possible to copy the newly created section into d.log?
I can think of one way which could be a possible solution to your problem.
"Maintain a log file which tracks lastModified date of each files and you can verify which file has been modified by using your log file.
--
Jitendra
No. you can not detect a file that will be modified. not until they come up with a highly accurate future predicting AI system.
your best approach would be to maintain a versioned backup of the the files. I would start with looking into some source code management system design considerations.
How would you know if the files are about to be modified? The system handles all of the file IO. The only way you could do that is to have the program doing the modification trigger the backup, and then make the modifications. For comparison, it depends on what you want. If you want a line-by-line comparison, that should be fairly simple to do using Java's file IO classes. If you just want to check if they are the same or not, you can use a checksum on both files.
I am writing a game in java, and i would like to save my game. I am going to save it as a set of XML documents in a zip file. What i would like to know is if their is a way i can do this transparently so that the user cannot edit these files, or find them. I have thought about just making the files hidden, but that seems like a dirty and ugly way to do this. The thing i like least about the hidden files is that they don't seem compatible between systems. If their was anyway to save the files to the jar and read them from it that would also be equally as acceptable. Also there doesn't seem like a good way in java to make a file hidden. Any help would be greatly appreciated.
What i would like to know is if their is a way i can do this transparently so that the user cannot edit these files, or find them.
Ultimately, no.
Anything that you do to store state on the local machine can ultimately be reverse engineered and that state can be retrieved by a privileged user.
But there are various things you could do to make it difficult for people to "cheat". For instance, you could encrypt the file, or generate a seeded hash to detect "tinkering".
Hiding files on Java 7 is simple:
Path path = FileSystems.getDefault().getPath("directory", "hidden.txt");
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
(Copied from: http://javarevisited.blogspot.com/2012/01/how-to-hide-file-java-program-example.html)
On earlier versions of Java you needed to call an external Windows application or call a native library to do it. But note that:
"hidden" files are only hidden from users who don't bother to look
they work differently on different platforms; e.g. for Linux/Unix based systems, "hidden" just means that the filename starts with a ".".
I'm writing a Java program and I'd like to convert a ogg file into mp3 file.
I've spend a lot of time trying to find a good library to do that, but without success for the moment.
I think I'll need a ogg decoder (jorbis ?) and a mp3 encoder (lameOnJ ?).
Moreover, once the conversion is done, I need to set some tags in the file (artist/track tag, etc).
This is a windows and OS X app.
Could you give me any hint about how to process, with examples if possible.
Thanks
You have lots of choices, and it depends on how much effort you want to put in, and what constraints you have regarding the execution platform.
Many developers would simply make System.exec() calls to external decode/encode/label executables, writing the intermediate files to disk. This is slightly clunky, but once it's set up properly, it works.
A more sophisticated option is to use libraries such as the ones you've found. You can still use the filesystem to temporarily store the uncompressed version.
You can, however, avoid storing the intermediate step -- and maybe make it faster -- by pipelining. You need to feed the output of the decoder as the input of the encoder, and set them both going.
The details of this depends on the API. If you're lucky, they can work with chunks, and you may be able to manage them in a single thread.
If they work with streams, you might need to get your hands dirty and work with threads. One thread for the encoder, one for the decoder.
I have this recurrent Java JAR program tasks that tries to modify a file every 60seconds.
Problem is that if user is viewing the file than Java program will not be able to modify the file. I get the typical IOException.
Anyone knows if there is a way in Java to modify a file currently in use? Or anyone knows what would be the best way to solve this problem?
I was thinking of using the File canRead(), canWrite() methods to check if file is in use. If file is in use then I'm thinking of making a backup copy of data that could not be written. Then after 60 seconds add some logic to check if backup file is empty or not. If backup file is not empty then add its contents to main file. If empty then just add new data to main file. Of course, the first thing I will always do is check if file is in use.
Thanks for all your ideas.
I was thinking of using the File
canRead(), canWrite() methods to check
if file is in use.
Not a good idea - you'll run into race conditions e.g. when your code has used those check methods, received true return values, but then the file is locked by a different application (possibly the user) just before you open it for writing.
Instead, try to get a FileLock on the file and use the "backup file" when that fails.
You can hold a lock on the file. This should guarantee you are able to write on the file.
See here on how to use the FileLock class.
If the user is viewing the file you should still be able to read it. In this case, make an exact copy of the file, and make changes to the new file.
Then after the next 60 seconds you can either:
1) Check if the file is being viewed and if not, delete it and replace it with the earlier file, then directly update this file.
2) If it is being viewed, continue making changes to the copy of the file.
EDIT: As Michael mentioned, when working with the main file, get a lock on it first.
I have been using JDOM library to read and write XML files through Java Servlets.
Problem is that when I send many requests using AJAX to my servlets which read and write data in XML files, many times it fails displaying error: Premature end of file. How can I synchronize all these read/writes to files or is there any other way to prevent these problem? Let me know if more details are required.
Will use of threads do any good in this situation?
Thanks a lot!
Two possible solutions are:
Write your files to thename.xml.part and then once done/closed to a rename to thename.xml, which makes the write closer to being atomic -- the reader can't read it until it's surely done so long as it is looking for ".xml" files only.
or, use a Database, atomicity is what they do.
I have faced such type of problem but exactly not the same. I am sharing my experience about this error. Please, sorry for any inconvenience.
I have faced the following problem
I have to form a xml file with dynamic variable data. And post that xml to a URL by PostMethod in java.
Normally It works. But when dynamic variable data is null. Then it shows the “Premature end of file".
Solution:
Just checking the variable is null or not. And it works for me.
Try adding all the write requests to a Process Queue and then build/append XML by those elements of Queue.
I have also faced this problem. My experience is...
Clean Project
Rebuild Project
then it will run successfully....