Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. package javaapplication1;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.text.*;
  5.  
  6. public class Main {
  7.  
  8. public static Set<obiekt> zbior = new LinkedHashSet<obiekt>();
  9. public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  10.  
  11. public static void main(String[] args) throws Exception
  12. {
  13.  
  14. File dir = new File("C:\\xampp\\tmp");
  15.  
  16. save(dir, "");
  17. readto("test.ser");
  18. readfrom("test.ser");
  19. }
  20.  
  21. private static void save(File dir, String path)
  22. {
  23. File[] dzieci = dir.listFiles();
  24. if(dzieci != null)
  25. {
  26. for(int i=0; i< dzieci.length; ++i)
  27. {
  28. String str = path + dzieci[i].getName();
  29. Date d = new Date(dzieci[i].lastModified());
  30.  
  31. if(dzieci[i].isDirectory() == true)
  32. {
  33. obiekt dziecko = new obiekt(str,sdf.format(d),dzieci[i].list().length, "folder");
  34. zbior.add(dziecko);
  35. save(dzieci[i], path + ">>");
  36. }
  37. else
  38. {
  39. obiekt dziecko = new obiekt(str,sdf.format(d),dzieci[i].length(), "plik");
  40. zbior.add(dziecko);
  41. }
  42.  
  43. }
  44. }
  45. }
  46. private static void readfrom(String path) throws Exception
  47. {
  48.  
  49. FileInputStream fis = new FileInputStream(path);
  50. ObjectInputStream ois = new ObjectInputStream(fis);
  51.  
  52. Set<obiekt> b = (LinkedHashSet<obiekt>) ois.readObject();
  53.  
  54. Iterator it=b.iterator();
  55.  
  56. while(it.hasNext())
  57. {
  58. obiekt value =(obiekt)it.next();
  59. System.out.println(value.nazwa + "\t" + value.data + "\t" + value.rozmiar + "\t" + value.type);
  60. }
  61.  
  62. ois.close();
  63. fis.close();
  64. }
  65.  
  66. private static void readto(String path) throws Exception
  67. {
  68. FileOutputStream fos = new FileOutputStream(path);
  69. ObjectOutputStream oos = new ObjectOutputStream(fos);
  70.  
  71. oos.writeObject(zbior);
  72.  
  73. oos.close();
  74. fos.close();
  75. }
  76. }
  77.  
  78. class obiekt implements Serializable
  79. {
  80. String nazwa;
  81. long rozmiar;
  82. String data;
  83. String type;
  84.  
  85. public obiekt(String n, String d, long r, String t)
  86. {
  87. nazwa = n;
  88. data = d;
  89. rozmiar = r;
  90. type = t;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement