Advertisement
desdemona

stara java

Feb 25th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package ls_in_java;
  6.  
  7. /**
  8.  *
  9.  * @author Dominika
  10.  */
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.ObjectInputStream;
  15. import java.io.ObjectOutputStream;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. public class Ls_in_java {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         // TODO code application logic here
  26.        
  27.         boolean sortuj = false;  
  28.         boolean czytaj = false;
  29.         boolean zapisz = false;
  30.         String nazwaPliku ="serializable.object";
  31.         //pierwszy argument to sortowanie. 1 dla tak, 0 dla nie.
  32.         //domyslnie 0
  33.        
  34.         if("1".equals(args[1]))
  35.         {
  36.             sortuj = true;
  37.         }
  38.        
  39.         if (args.length == 3)
  40.         {
  41.             if (args[2].equals("czytaj")) czytaj = true;
  42.             else if (args[2].equals("zapisz")) zapisz = true;
  43.         }
  44.        
  45.         //drugi to czytaj albo zapisz
  46.         //formalnie trzeci
  47.         //bo to sciezka jest pierwszym
  48.        
  49.        
  50.         if(zapisz == true) // serializacja
  51.         {
  52.             Plikofolder obiekt = new Plikofolder(args[0], sortuj); // czytanie zawartosci katalogu
  53.             obiekt.print(0); // wyswietlenie zawartosci katalogu
  54.  
  55.             ObjectOutputStream oos = null;
  56.             try
  57.             {
  58.                 oos = new ObjectOutputStream(new FileOutputStream(nazwaPliku));
  59.                 oos.writeObject(obiekt);
  60.             }
  61.             catch (IOException e)
  62.             {
  63.                 System.out.println(e.getMessage());
  64.             }
  65.             finally
  66.             {
  67.                 try
  68.                 {
  69.                     if (oos != null) oos.close();
  70.                 }
  71.                 catch (IOException e)
  72.                 {
  73.                     System.out.println(e.getMessage());
  74.                 }
  75.             }
  76.         }
  77.         else if(czytaj == true) // deserializacja
  78.         {
  79.             Plikofolder obiekt = null;
  80.             ObjectInputStream ois = null;
  81.  
  82.             try
  83.             {
  84.                 ois = new ObjectInputStream(new FileInputStream(nazwaPliku));
  85.                 try {
  86.                     obiekt = (Plikofolder)ois.readObject();
  87.                 } catch (ClassNotFoundException ex) {
  88.                     Logger.getLogger(Ls_in_java.class.getName()).log(Level.SEVERE, null, ex);
  89.                 }
  90.  
  91.             }
  92.             catch (IOException e)
  93.             {
  94.                 System.out.println(e.getMessage());
  95.             }
  96.             finally
  97.             {
  98.                 try
  99.                 {
  100.                     if (ois != null)
  101.                     {
  102.                         ois.close();
  103.                         obiekt.print(0); // wypisanie wczytanego zdeserializowanego obiektu
  104.                     }
  105.                 }
  106.                 catch (IOException e)
  107.                 {
  108.                     System.out.println(e.getMessage());
  109.                 }
  110.             }
  111.         }
  112.         else
  113.         {
  114.             Plikofolder obiekt = new Plikofolder(args[0], sortuj); // wczytanie zawartosci katalogu
  115.             obiekt.print(0); // wyswietlenie zawartosci
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement