Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. package IOmanager;
  2. import data.ControlData;
  3. import data.Sheet;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.PrintWriter;
  14. import java.util.ArrayList;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /*
  19. * To change this license header, choose License Headers in Project Properties.
  20. * To change this template file, choose Tools | Templates
  21. * and open the template in the editor.
  22. */
  23.  
  24. /**
  25. *
  26. * @author lcsvcn
  27. */
  28. public class IO_Manager {
  29. private static final String src = "src/IOmanager/file.bin";
  30. private ArrayList<Sheet> al = null;
  31. private boolean hasArray;
  32. private ControlData cd;
  33. public IO_Manager(ControlData cd) {
  34. this.cd = cd;
  35. loadArray();
  36. }
  37.  
  38. private void saveArray() {
  39.  
  40. if(al.isEmpty()) {
  41. System.out.println("ArrayList is empty");
  42. } else if(al == null) {
  43. System.out.println("ArrayList is null send some array");
  44. } else {
  45. File fout = new File(src);
  46. FileOutputStream fos = null;
  47. try {
  48. fos = new FileOutputStream(fout);
  49. } catch (FileNotFoundException ex) {
  50. System.out.println("FileNotFoundException");
  51. Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
  52. }
  53.  
  54. try {
  55.  
  56.  
  57. try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(src, true)))) {
  58. // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  59. // try {
  60. for(Sheet s : al) {
  61.  
  62. // Cod Ficha
  63. System.out.println("w: "+ s.getSheetID());
  64.  
  65. out.println(Integer.toString(s.getSheetID()));
  66. // bw.newLine();
  67.  
  68. // Nome
  69. System.out.println("w: " + s.getName());
  70.  
  71. out.println(s.getName());
  72. // bw.newLine();
  73.  
  74. // Idade
  75. System.out.println("w: " + s.getAge());
  76.  
  77. out.println(Integer.toString(s.getAge()));
  78. // bw.newLine();
  79. // Plano de Saude
  80. System.out.println("w: "+ s.getHealthyCarePlan());
  81.  
  82. out.println(Integer.toString(s.getHealthyCarePlan()));
  83. // bw.newLine();
  84.  
  85. // Estado Civil
  86. System.out.println("w: "+ s.getMaritalStatus());
  87.  
  88. out.println(Integer.toString(s.getMaritalStatus()));
  89.  
  90. // bw.newLine();
  91.  
  92. }
  93.  
  94. out.close();
  95. } catch (IOException ex) {
  96. System.out.println("IOException");
  97. Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99.  
  100. System.out.println("saved");
  101. } catch (NullPointerException e) {
  102. System.out.println("fucked");
  103. }
  104. }
  105. }
  106. private boolean loadArray() {
  107. File fin = new File(src);
  108. FileInputStream fis = null;
  109. String name="erro";
  110. int age=-1;
  111. int sId=-1;
  112. int hCarePlan=-1;
  113. int mStatus=-1;
  114.  
  115. int cont = 0;
  116. int num = 5;
  117. int reg = 0;
  118.  
  119. try {
  120. fis = new FileInputStream(fin);
  121. } catch (FileNotFoundException ex) {
  122. System.out.println("File to load not found!");
  123. return false;
  124. }
  125.  
  126. //Construct BufferedReader from InputStreamReader
  127. BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  128.  
  129. System.out.println("-------------");
  130. System.out.println("inicio da leitura");
  131. System.out.println("-------------");
  132.  
  133. String line = null;
  134. try {
  135. System.out.println("Reg num: " + reg);
  136.  
  137. while((line = br.readLine()) != null) {
  138. switch(cont) {
  139. case 0:
  140. sId = Integer.valueOf(line);
  141. System.out.println("r: " + sId);
  142. break;
  143. case 1:
  144. name = line;
  145. System.out.println("r: " + name);
  146. break;
  147. case 2:
  148. age = Integer.valueOf(line);
  149. System.out.println("r: " + age);
  150. break;
  151. case 3:
  152. hCarePlan = Integer.valueOf(line);
  153. System.out.println("r: " + hCarePlan);
  154. break;
  155. case 4:
  156. mStatus = Integer.valueOf(line);
  157. System.out.println("r: " + mStatus);
  158. break;
  159. }
  160. // Fim de um registro e comeco de outro
  161. if(cont >= num) {
  162. cd.sendData(name, age, mStatus, hCarePlan, sId);
  163. cont = 0;
  164. reg++;
  165. System.out.println("-------------");
  166. System.out.println("Reg num: " + reg);
  167. }
  168. cont++;
  169. }
  170. br.close();
  171. System.out.println("-------------");
  172. System.out.println("fim da leitura");
  173. System.out.println("-------------");
  174. } catch (IOException ex) {
  175. Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
  176. }
  177. System.out.println("load");
  178. return true;
  179.  
  180. }
  181.  
  182. public void setArray(ArrayList<Sheet> mList) {
  183. al = mList;
  184. saveArray();
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement