Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4.  
  5. public class Main {
  6.  
  7. private static String DATAIN = "data/dirfrom";
  8. private static String DATATO = "data/dirto/";
  9.  
  10. public static void main(String[] args) throws IOException {
  11. findDocuments(DATAIN, DATATO);
  12. }
  13.  
  14. public static void findDocuments(String in, String out) throws IOException {
  15. File fin = new File(in);
  16. File fout = new File(out);
  17.  
  18. File[] alldat = fin.listFiles(new FilenameFilter() {
  19. @Override
  20. public boolean accept(File dir, String name) {
  21. return name.endsWith(".xlsx") || name.endsWith(".doc");
  22. }
  23. });
  24.  
  25.  
  26. if (!fin.exists()) {
  27. System.out.println("ne postoi");
  28. return;
  29. }
  30. if (fout.exists()) {
  31. for (File f : fout.listFiles()) {
  32. f.delete();
  33. }
  34. }
  35.  
  36. if (alldat.length > 0) {
  37. for (File f : alldat) {
  38. if (f.length() > 500000) {
  39. moveFile(f, out+"//dokumenti");
  40. } else{
  41. writeToDatabase(f,out+"//brisenje.txt");
  42. }
  43. }
  44. } else {
  45. System.out.println("Ne postoi nitu eden file so ekstenzija .xls ili .doc");
  46. }
  47.  
  48. }
  49.  
  50. private static void writeToDatabase(File f, String out) throws IOException {
  51.  
  52. try(
  53. RandomAccessFile randomAccessFile = new RandomAccessFile(out,"rw");
  54.  
  55. ){
  56. randomAccessFile.seek(0);
  57. randomAccessFile.writeUTF(f.getName());
  58. randomAccessFile.writeInt((int) (f.length() * 1000));
  59.  
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66. public static boolean moveFile(File file, String newparent) {
  67. return moveFileandRename(file, newparent, file.getName());
  68. }
  69.  
  70. public static boolean moveFileandRename(File file, String newParent, String newName) {
  71. File parent = new File(newParent);
  72.  
  73.  
  74. if (!parent.isDirectory()) {
  75. System.out.println("Ne Postoi PARENT!");
  76. return false;
  77. }
  78.  
  79. File renamedfile = new File(parent, newName);
  80.  
  81. if (renamedfile.exists()) {
  82. System.out.println("OVOJ FILE VEKE POSTOI");
  83. return false;
  84. }
  85.  
  86. return file.renameTo(renamedfile);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement