Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package u.ds.timetracker;
  2.  
  3.  
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import java.io.ObjectOutputStream;
  12. import java.io.PrintStream;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Collection;
  15. import java.util.Date;
  16.  
  17. public class MainApplication {
  18. public static void main(String[] args) throws InterruptedException, FileNotFoundException {
  19.  
  20. Date dateActual = new Date();
  21. SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy");
  22. sdf.format(dateActual);
  23.  
  24. PrintStream originalStream;
  25. PrintStream newStream;
  26. ByteArrayOutputStream newConsole = new ByteArrayOutputStream();
  27.  
  28. //Instanciem el projecte p1 i iniciliatzem les seves tasques
  29. Project p1 = new Project("p1", null);
  30. Task t3 = new Task("t3", p1);
  31.  
  32. //Instanciem el projecte p2 i iniciliatzem les seves variables
  33. Project p2 = new Project("p2", p1);
  34. Task t1 = new Task("t1", p2);
  35. //Task t4 = new Task("t4");
  36. Task t2 = new Task("t2", p2);
  37.  
  38.  
  39. //Creem la estructura del arbre
  40. p1.addTask(t3);
  41. p2.addTask(t1);
  42. p2.addTask(t2);
  43. p1.addProject(p2);
  44.  
  45.  
  46. //creem e iniciem el thread on estara el rellotge
  47. Runnable r = new GenTiks();
  48. Thread t = new Thread(r);
  49. t.start();
  50.  
  51. // en el bucle d'inicien i es paren les tasques
  52. /*while (true) {
  53.  
  54. //Fita 1
  55. System.out.println("\n");
  56. System.out.println("El temps es comptara cada 2 segons");
  57. //Aquest Sleep el posem perque el rellotge tingui temps de fer el primer tik
  58. Thread.sleep(100);
  59. p1.start("t3");
  60. Thread.sleep(3000);
  61. p1.parar("t3");
  62. Thread.sleep(7000);
  63. p1.start("t2");
  64. Thread.sleep(10000);
  65. p1.parar("t2");
  66. //Thread.sleep(2000);
  67. p1.start("t3");
  68. Thread.sleep(2000);
  69. p1.parar("t3");
  70. Thread.sleep(400000000); //FITA 1
  71. */
  72.  
  73. //Aquest es el codi per fer el test A.2
  74. /*Thread.sleep(100);
  75. p1.start("t3");
  76. Thread.sleep(4000);
  77. p1.start("t2");
  78. Thread.sleep(2000);
  79. p1.parar("t3");
  80. Thread.sleep(2000);
  81. p1.start("t1");
  82. Thread.sleep(4000);
  83. p1.parar("t1");
  84. Thread.sleep(2000);
  85. p1.parar("t2");
  86. Thread.sleep(4000);
  87. p1.start("t3");
  88. Thread.sleep(2000);
  89. p1.parar("t3");
  90. Thread.sleep(400000000);*/
  91.  
  92.  
  93. //SERIALITZEM EL OBJECTE P1
  94.  
  95. /*
  96. try {
  97.  
  98. // Escrivim
  99. ObjectOutputStream escrivint = new ObjectOutputStream(new FileOutputStream("C:/Users/Jordi/Desktop/UAB/Curs 3/1rSemestre/DS/Practiques/data.txt"));
  100. escrivint.writeObject(p1);
  101. escrivint.close();
  102. // Recuperem
  103. ObjectInputStream recuperant = new ObjectInputStream(new FileInputStream("C:/Users/Jordi/Desktop/UAB/Curs 3/1rSemestre/DS/Practiques/data.txt"));
  104. Project recuperant_projecte = (Project) recuperant.readObject();
  105. recuperant.close();
  106. } catch (Exception e) {
  107. System.out.println(e);
  108.  
  109. }
  110. */
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. Activity activity = new Project("p4",p2);
  120.  
  121.  
  122. try{
  123. FileInputStream fitxerEntrada = new FileInputStream("data.txt");
  124. ObjectInputStream entrada = new ObjectInputStream(fitxerEntrada);
  125. System.out.println("bla bla bla");
  126. } catch(IOException e){
  127. System.out.println(e.getMessage());
  128. }
  129.  
  130.  
  131. try{
  132. FileOutputStream fitxerSortida = new FileOutputStream("data.txt");
  133. ObjectOutputStream sortida = new ObjectOutputStream(fitxerSortida);
  134. sortida.close();
  135. fitxerSortida.close();
  136. System.out.println("bla bla bla");
  137. } catch(FileNotFoundException e){
  138. e.printStackTrace();
  139. } catch(IOException e){
  140. System.out.println(e.getMessage());
  141. }
  142.  
  143.  
  144. // Deserialization
  145. try {
  146. FileInputStream fileIn = new FileInputStream("data.txt");
  147. ObjectInputStream in = new ObjectInputStream(fileIn);
  148. //c.voidProject.setChildren((Collection<Activity>) in.readObject());
  149. System.out.println("Desarialized data from data.txt");
  150. } catch (IOException e) {
  151. System.out.println("There is not data.txt file created.");
  152. }
  153.  
  154. // Serialization
  155. try {
  156. FileOutputStream fileOut = new FileOutputStream("data.txt");
  157. ObjectOutputStream out = new ObjectOutputStream(fileOut);
  158. out.writeObject(activity);
  159. out.close();
  160. fileOut.close();
  161. System.out.println("Serialized data is saved in data.txt");
  162. System.out.println("Serialized data is saved in data.txt");
  163. } catch (FileNotFoundException e) {
  164. e.printStackTrace();
  165. System.out.println("There is not data.ser file created.");
  166. } catch (IOException e) {
  167. System.out.println("An error has ocurred when we tried to serialize.");
  168. }
  169.  
  170. }
  171.  
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement