I did a quick search and found this library that might be useful for what you need: Jnotify
EDIT:
I was interested in being something that I could use in the future both pure java and java/android...
So I did some tests and I’ll put here all the steps I did using the Jnotify to monitor a folder or file, Servant, Modified, Deleted or Renamed
Configuring
Extract from the file .zip
the file .jar
and the file with the name jnotify.dll
for systems 32 bits and jnotify_64bit.dll
for systems 64 bits
IN THE ECLIPSE - After creating the project add the library jar to the project example here, then add to dll following these steps:
- Create a folder in the project with name
dll
- Put the file . dll inside
- In the project -> properties -> Java Build Path -> tab: Source click on your/src project and expand
- Click on Native library Location and then on the EDIT button next to it
- Click the Workspace button and select the folder
dll
created in the first step.
IN NETBEANS - Add the .jar to the project as explained at that link, and for the part of dll i found the following steps:
- Access the project properties
- Click on RUN
- In VM Options, please sort: -Djava.library.path="C: Directory where the DLL is from"
Code
This part has no mystery, I created a class only with the method main
and what it needed as it already shows in the examples of the site itself Jnotify.
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
public class Main {
public static void main(String[] args) {
try {
observarPasta();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void observarPasta() throws Exception {
// criei uma pasta temp para observar os arquivos que forem
// criados e alterados dentro dela, mas pode ser alterado
// para um arquivo especifico
String path = "C:\\temp";
// a mask é as ações que vão ser observadas, mas pode
// ser utilizado JNotify.FILE_ANY para monitorar tudo
// também.
int mask = JNotify.FILE_CREATED |
JNotify.FILE_DELETED |
JNotify.FILE_MODIFIED |
JNotify.FILE_RENAMED;
// monitorar subPastas?
boolean watchSubtree = true;
// adiciona o "MONITORADOR"
int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
//Fica esperando um tempo até terminar a aplicação
//Dependendo da implementação da sua aplicação
//isso não será necessário mas para esse teste é interessante
Thread.sleep(1000000);
// aqui remove o seu "MONITORADOR"
boolean res = JNotify.removeWatch(watchID);
if (!res) {
// o id foi inválido.
}
}
//essa implementação ja se explica com seus métodos
static class Listener implements JNotifyListener {
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
print("renomeado " + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
print("modificado " + rootPath + " : " + name);
}
public void fileDeleted(int wd, String rootPath, String name) {
print("deletado " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
print("criado " + rootPath + " : " + name);
}
void print(String msg) {
System.err.println(msg);
}
}
}
I could show you what you’ve done to get help.
– durtto
I published in the topic below the program.
– Alex Inoue
What do you mean? You should put it in your question, not as an answer. You created an answer and not a topic.
– durtto
Sorry, it’s just that I’m new to the forum. I tried to copy here but it was too long.
– Alex Inoue
All right, you get used to it. Here is not a Forum, it is a question and answer site.
– durtto
Okay. You were able to help me?
– Alex Inoue
you can create a function that reads the file from time to time, if the file has a size other than the previous one, then run the import.
– durtto
Search for the Observer class and its use, in your case, it can help to do this monitoring automatically.
– user28595