Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.contentobjects.jnotify.JNotify;
- import net.contentobjects.jnotify.JNotifyListener;
- import java.io.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.util.Iterator;
- import java.util.StringTokenizer;
- 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:\Users\solutel\Documents\tabela\";
- // 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);
- String dir = "C:\Users\solutel\Documents\tabela";
- File file = new File(dir);
- for(String arq : file.list()){
- if(arq.endsWith(".txt")){
- try{
- System.out.println(leitura(dir + "\" + arq));
- }
- catch(Exception ex){}
- }
- }
- private static String leitura(String dir) throws Exception {
- Class.forName("org.gjt.mm.mysql.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Radius","root","admin");
- Statement stm = con.createStatement();
- BufferedReader reader = new BufferedReader(new FileReader(new File(dir)));
- String dados[] = new String[3];
- String linha = "", conteudo = "";
- while((linha = reader.readLine()) != null){
- StringTokenizer st = new StringTokenizer(linha,";"");
- dados[0] = st.nextToken();
- dados[1] = st.nextToken();
- }
- stm.executeUpdate("insert into radcheck (username,attribute,op,value) values ('"+dados[0]+"','" +"User-Password"+ "','"+ ":="+ "','"+dados[1]+"')");
- linha = reader.readLine();
- if(!linha.isEmpty()){
- conteudo = new StringBuilder(conteudo).append(linha.concat("n")).toString();
- }
- reader.close();
- return conteudo;
- }
- void print(String msg) {
- System.err.println(msg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement