Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import net.contentobjects.jnotify.JNotify;
  2. import net.contentobjects.jnotify.JNotifyListener;
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.Statement;
  7. import java.util.Iterator;
  8. import java.util.StringTokenizer;
  9.  
  10.  
  11. public class Main {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. try {
  16. observarPasta();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }
  21.  
  22. public static void observarPasta() throws Exception {
  23.  
  24. // criei uma pasta temp para observar os arquivos que forem
  25. // criados e alterados dentro dela, mas pode ser alterado
  26. // para um arquivo especifico
  27.  
  28. String path = "c:\Users\solutel\Documents\tabela\";
  29.  
  30. // a mask é as ações que vão ser observadas, mas pode
  31. // ser utilizado JNotify.FILE_ANY para monitorar tudo
  32. // também.
  33.  
  34. int mask = JNotify.FILE_CREATED |
  35. JNotify.FILE_DELETED |
  36. JNotify.FILE_MODIFIED |
  37. JNotify.FILE_RENAMED;
  38.  
  39. // monitorar subPastas?
  40. boolean watchSubtree = true;
  41.  
  42. // adiciona o "MONITORADOR"
  43. int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
  44.  
  45. //Fica esperando um tempo até terminar a aplicação
  46. //Dependendo da implementação da sua aplicação
  47. //isso não será necessário mas para esse teste é interessante
  48. Thread.sleep(1000000);
  49.  
  50. // aqui remove o seu "MONITORADOR"
  51. boolean res = JNotify.removeWatch(watchID);
  52.  
  53. if (!res) {
  54. // o id foi inválido.
  55. }
  56. }
  57.  
  58. //essa implementação ja se explica com seus métodos
  59. static class Listener implements JNotifyListener {
  60. public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
  61. print("renomeado " + rootPath + " : " + oldName + " -> " + newName);
  62. }
  63.  
  64.  
  65. public void fileModified(int wd, String rootPath, String name) {
  66. print("modificado " + rootPath + " : " + name);
  67. }
  68.  
  69. public void fileDeleted(int wd, String rootPath, String name) {
  70.  
  71. print("deletado " + rootPath + " : " + name);
  72.  
  73. }
  74.  
  75. public void fileCreated(int wd, String rootPath, String name) {
  76.  
  77. print("criado " + rootPath + " : " + name);
  78.  
  79. String dir = "C:\Users\solutel\Documents\tabela";
  80. File file = new File(dir);
  81. for(String arq : file.list()){
  82. if(arq.endsWith(".txt")){
  83.  
  84. try{
  85. System.out.println(leitura(dir + "\" + arq));
  86. }
  87. catch(Exception ex){}
  88. }
  89. }
  90.  
  91. private static String leitura(String dir) throws Exception {
  92.  
  93.  
  94. Class.forName("org.gjt.mm.mysql.Driver");
  95. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Radius","root","admin");
  96. Statement stm = con.createStatement();
  97.  
  98. BufferedReader reader = new BufferedReader(new FileReader(new File(dir)));
  99.  
  100. String dados[] = new String[3];
  101. String linha = "", conteudo = "";
  102.  
  103. while((linha = reader.readLine()) != null){
  104. StringTokenizer st = new StringTokenizer(linha,";"");
  105. dados[0] = st.nextToken();
  106. dados[1] = st.nextToken();
  107. }
  108.  
  109. stm.executeUpdate("insert into radcheck (username,attribute,op,value) values ('"+dados[0]+"','" +"User-Password"+ "','"+ ":="+ "','"+dados[1]+"')");
  110. linha = reader.readLine();
  111. if(!linha.isEmpty()){
  112. conteudo = new StringBuilder(conteudo).append(linha.concat("n")).toString();
  113. }
  114.  
  115.  
  116. reader.close();
  117. return conteudo;
  118. }
  119.  
  120. void print(String msg) {
  121. System.err.println(msg);
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement