Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. package pigment;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. class Virtually_library
  13. {
  14.     private int size_of_library;                           //zmienna przechowująca liczbę pozycji wirtu-
  15.     private String library_source;                      //alnej biblioteki; łańcuch przechowujący adres
  16.     private final ArrayList<Item> library;       //biblioteki; lista przechowująca pozycje biblio-
  17.                                                                                    //teki, obiekty typu Item- wirtualny reprezentant
  18.     Virtually_library()                                            //biblioteki; konstruktor klasy tworzy nową listę,
  19.     {                                                                                          //przypisuje zmiennej przechowującej liczebność
  20.         this.size_of_library=0;                                             //biblioteki wartość początkową oraz ustawia    
  21.         this.library = new ArrayList<>();                         //adres biblioteki
  22.         select_library();
  23.     }
  24.    
  25.     void select_library()           //metoda pozyskująca adres biblioteki od użytkownika
  26.     {
  27.         Scanner reader=new Scanner(System.in);
  28.         String path;
  29.         System.out.println("Wybierz adres biblioteki");
  30.         path=reader.nextLine();
  31.         this.library_source=path;
  32.     }
  33.  
  34.     void delete_library()   //metoda usuwająca wszystkie pozycje wirtualnej
  35.     {               //biblioteki, przypisując tym samym wartość 0 roz-
  36.         library.removeAll(library);     //miarowi biblioteki
  37.         this.size_of_library=library.size();
  38.     }
  39.  
  40.     void load_library()                 //ładowanie wirtualnej biblioteki na podstawie plików z folderu
  41.     {               //plików znajdujących się w folderze o zadanym
  42.         delete_library();           //adresie przechowywanym w zmiennej library_so-
  43.         File folder=new File(library_source);                            //urce
  44.         File[] list_of_files=folder.listFiles();
  45.         int s=0;
  46.  
  47.         for (File list_of_file: list_of_files)
  48.         {
  49.             if (list_of_file.isFile())
  50.             {
  51.                 Item position=new Item(list_of_file.getAbsolutePath(), null);
  52.                 position.set_everything();                      
  53.                 this.library.add(position);
  54.                 s++;
  55.             }
  56.         }
  57.  
  58.         this.size_of_library=s;                                                        //wraz z dodaniem do listy nowej pozycji, zwię-
  59.     }                                //biblioteki, zwiększa się rozmiar wirtualnej biblioteki
  60.  
  61.     void refresh_library()  //metoda odświeżająca wirtualną bibliotekę
  62.     {
  63.         delete_library();
  64.         load_library();
  65.     }
  66.  
  67.     int get_size_of_library()   //metoda zwracajaca rozmiar biblioteki
  68.     {
  69.         return library.size();
  70.     }
  71.  
  72.     boolean check_if_exists(String name)      //metoda sprawdzająca czy w folderze przechowu-
  73.     {                                                                                                   //jącym pliki widmowe istnieje plik o zadanej na-
  74.         File folder=new File(library_source);                           //zwie
  75.         File[] listoffiles=folder.listFiles();
  76.         boolean success=false;
  77.  
  78.         for (File listoffile: listoffiles)
  79.         {
  80.             String name1 = listoffile.getName();
  81.             if(name.equals(name1))
  82.             {
  83.                 success=true;
  84.                 break;
  85.             }
  86.  
  87.             else
  88.             {
  89.                 success=false;
  90.             }
  91.         }
  92.  
  93.         return success;
  94.     }
  95.  
  96.     void copy_file(String path) //metoda kopiująca plik do folderu biblioteki
  97.     {
  98.         InputStream inStream;
  99.         OutputStream outStream;
  100.         File namex=new File(path);
  101.         String name=namex.getName();
  102.  
  103.         try
  104.         {
  105.             File afile=new File(path);
  106.             File bfile=new File(library_source+"\\"+name);
  107.  
  108.             inStream=new FileInputStream(afile);
  109.             outStream=new FileOutputStream(bfile);
  110.  
  111.             byte[] buffer=new byte[1024];
  112.  
  113.             int length;
  114.  
  115.             while ((length=inStream.read(buffer))>0)
  116.             {
  117.                 outStream.write(buffer, 0, length);
  118.             }
  119.  
  120.             inStream.close();
  121.             outStream.close();
  122.         }
  123.  
  124.         catch(IOException e)
  125.         {
  126.             e.printStackTrace();
  127.         }
  128.     }
  129.  
  130.     void add_item(String path)  //metoda dodająca plik do biblioteki plików
  131.     {
  132.         File x=new File(path);
  133.         File folder=new File(library_source);
  134.         File[] listoffiles=folder.listFiles();
  135.  
  136.         if(listoffiles.length==0)
  137.         {
  138.             copy_file(path);
  139.         }
  140.  
  141.         else if(!check_if_exists(x.getName()))
  142.         {
  143.             copy_file(path);
  144.             System.out.println("This file is on your data base now");
  145.         }
  146.  
  147.         else
  148.         {
  149.             System.out.println("This file already exists");
  150.         }
  151.  
  152.       refresh_library();
  153.     }
  154.  
  155.     void delete_item(String name)   //metoda usuwająca plik o zadanej nazwie z bib-
  156.     {           //blioteki plików
  157.         try
  158.         {
  159.             File file=new File(library_source+"\\"+name);
  160.  
  161.             if(check_if_exists(name))
  162.             {
  163.                 if(file.delete())
  164.                 {
  165.                     System.out.println(file.getName()+" is deleted!");
  166.                 }
  167.  
  168.                 else
  169.                 {
  170.                     System.out.println("Something went wrong.");
  171.                 }
  172.             }
  173.  
  174.             else
  175.             {
  176.                 System.out.println("This file doesn't exist");
  177.             }
  178.         }
  179.  
  180.         catch(Exception e)
  181.         {
  182.             e.printStackTrace();
  183.         }
  184.  
  185.         refresh_library();
  186.     }
  187.  
  188.     ArrayList get_list()    //metoda zwracająca listę obiektów wirtualnej
  189.     {       //biblioteki
  190.         load_library();
  191.         return library;
  192.     }
  193.    
  194.     ArrayList<String> check_similarity(String path, String back)         //metody analizujące widma
  195.     {
  196.         ArrayList a=get_list();
  197.         return Counting.check_similarity(path, back, a);
  198.     }
  199.    
  200.     ArrayList<String> count_SAM(String path, String back)
  201.     {
  202.         ArrayList a=get_list();
  203.         return Counting.count_SAM(path, back, a);
  204.     }
  205.    
  206.     ArrayList<String> check_containing(String path, String back)
  207.     {
  208.         ArrayList a=get_list();
  209.         return Counting.check_containing(path, back, a, "SAM");
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement