How to poll directory to check whether new file is added? - java

I want to poll a directory to check whether new file is added to the directory. If any new file is added I want to read that file.
Can anybody give me an idea how to do that?

Java 7 has a file watcher API
JNotify will do it as well.

If you are using Java 7, you can use the filesystem watch service (a new feature in Java 7).
See Oracle's tutorial that explains how to use it.
Otherwise (if you're using an older Java version) you can use a library such as Apache Commons IO. Look at the package org.apache.commons.io.monitor - it has classes to check for changes in files and directories.

jNotify would be useful,
See Also
directory-listener-in-java

Why not Apache Camel?
Here's what the code will look like:
from("file://pollingfolder?delete=true").to("bean:handleOrder");
That reads the files from "pollingfolder", deletes it upon read, and sends it to a bean called "handleOrder". Just in one line!
There's an easy way to configure it with Spring-boot automagically if you use Spring, but it can be used in plain Java as well.
Source: Apache Camel

Related

What method Apache camel uses to delete the file using delete=true, is it JDK FileUtil.deleteFile(file)

I have been asked to provide details on how Apache Camel delete functionality works behind the scene, the method used and if the deleted file can be recovered as we are deleting PCI file. Based on my understanding, the files once deleted cant be recovered but is their a documentation that proves the same from Apache Camel.
I think i have found the answer:- the files are deleted using java.io.File#delete() method, where calling File.delete() permanently removes the file, without going into the recycling bin or anything similar.

In-memory file system in java

I want to create a simple in-memory file system in Java, which has one root directory and is able to make new sub directory. In the directory we can make new files, write in them, read from them, delete them and also rename them. Can you please give some advice from where to start (a simple code, or resouce).
A custom file system provider must implement the java.nio.file.spi.FileSystemProvider class. A file system provider is identified by a URI scheme such as file, jar, memory, cd.
These links below provide good starting info
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html
The link below(not about in memory file system) is about virtual file system. It talks about some design issues which could help you in case you decide to create your own file system.
http://www.flipcode.com/archives/Programming_a_Virtual_File_System-Part_I.shtml
But you could always use already built and tested code.This will be faster and easier to maintain and you will receive support in error conditions.
Take a look at jimfs ( An in-memory file system for Java 7+ )
https://github.com/google/jimfs
Also look into
Commons Virtual File System
http://commons.apache.org/proper/commons-vfs/
marschall (An in memory implementation of a JSR-203 file system)
https://github.com/marschall/memoryfilesystem
You can create In-memory file system in java using Google’s Jimfs and java 7 NIO package.
Please refer this link. Here you will get a sample tutorial:
create In-memory file system in java
Use memoryfilesystem.
Jimfs has been mentioned in a previous answer, but memoryfilesystem handles much more.
Example usage:
final FileSystem fs = MemoryFileSystem.newLinux().build("myfs");
final Path dir = fs.getPath("thedir");
Files.createDirectory(dir);
etc etc. Use the java.nio.file API to manipulate files in this (File won't work!). See here for more details.

Windows - get warned when a file is created

I'm not sure if what I'm asking is possible, but I would like to do the following:
When a file is created in a certain folder (Windows), my program should respond. I'd like to let Windows call a callback method when a file is created.
Another option is of course just use a loop and constantly check if a new file is in the folder, but I'd like to know it instantly, so a callback method would be much more efficient.
Is this possible? The language is not important, although Java is preferred.
With Java nio 2 (available in Java 1.7 +), you can "watch" a directory and get notified when that directory changes.
The method proposed in the tutorial linked above uses the WatchService API.
Commons IO contains a FileAlterationListener wich has a onDirectoryChangemethod. Can be an alternative if Java 1.7 is not available.
If you are not bound to Java, then you could use very convenient FileSystemWatcher in C# or VisualBasic. It will allow you to watch all kinds of events which can occur in folder and it's quite easy to implement it.

Using java to represent a file structure in memory?

I'm trying to index a file structure (not actual data) relative to a specific path on my local disk. Initially I load the file structure to memory then listen for real changes in the directory. If one occurs then I update the relevant indexed files in internal memory. Not unlike what dropbox seems to do?
Does there exist any java-library which can do this? I don't really want to implement this from scratch.
To listen for changes on a file, you can have a look at apache commons io which has a FileAlterationMonitor.
Java 7, as part of NIO.2, has the WatchService API.
The WatchService API is designed for applications that need to be notified about file change events.

Java library to create cabinet files on Unix?

Is there a Java library to create cabinet files on Unix. I don't need any compression support. I just want to create a plain cab file using Java.
Something similar to cablib (http://sourceforge.net/projects/cablib/) which can only be used for reading cab files would be perfect.
If there is really no library can I use a feasible work around? E.g. create a ZIP file and somehow convert it into a CAB file?
If there is really no library can I use a feasible work around?
Comments have suggested using the Linux Icab tool.
E.g. create a ZIP file and somehow convert it into a CAB file?
The ZIP file format is different in too many respects for there to be a simple transformation to turn a ZIP file into a CAB file.
Edit: The answer below isn't a pure java solution. Ant's CAB task documentation says it relies on a 3rd-party tool: Either MS's "CABARC" or the open-source "libcabinet", which seems to no longer exist. So there is no benefit to this approach compared to a 3rd-party system call.
Previous Answer (read above first):
If you need a pure java way of creating cab files (not extracting them), you can use ant's built-in "cab" task.
This gives you a few options:
Call the ant task from within your java code by using ant's
Launcher class;
Find the source code for the task definition (here) , and remove references to the ant context to create your own Cab extract utility
Run ant via a system call.

Categories