Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package Auxiliar;
  2. import java.util.ArrayList;
  3. import java.util.Enumeration;
  4. import java.util.Hashtable;
  5. import java.util.List;
  6. import java.io.*;
  7.  
  8. public class FileHandler {
  9.  
  10. private BufferedReader fRead;
  11. private BufferedWriter fWriter;
  12.  
  13. public List<String> readFile(String ficheiro) throws IOException {
  14. List<String> lines = new ArrayList<String>();
  15. String line;
  16.  
  17. //abrir ficheiro para leitura
  18. this.abreLeitura(ficheiro);
  19.  
  20. //ler linha a linha
  21. for(int i=0; ;i++) {
  22. line = readOneLine();
  23. if(line == null) {
  24. break;
  25. }
  26. lines.add(line);
  27. continue;
  28. }
  29. //fechar ficheiro para leitura
  30. this.fechaLeitura();
  31. return lines;
  32. }
  33.  
  34. public void writeFile(Hashtable<String, String> hash, String file) throws IOException {
  35. abreEscrita(file);
  36.  
  37. String kk;
  38. Enumeration<String> e = hash.keys();
  39. while(e.hasMoreElements()){
  40. kk = (String)e.nextElement();
  41. writeLine(kk+"->"+hash.get(kk));
  42. }
  43. fechaEscrita();
  44. }
  45.  
  46. private void abreLeitura(String nomeDoFicheiro) throws IOException {
  47. File f = new File(nomeDoFicheiro);
  48. fRead = new BufferedReader(new FileReader(f.getAbsolutePath()));
  49. }
  50.  
  51.  
  52. private String readOneLine() throws IOException {
  53. return fRead.readLine();
  54. }
  55.  
  56. private void fechaLeitura() throws IOException {
  57. fRead.close();
  58. }
  59.  
  60. private void abreEscrita(String nomeDoFicheiro) throws IOException {
  61. File f = new File(nomeDoFicheiro);
  62. fWriter = new BufferedWriter(new FileWriter(f.getAbsolutePath()));
  63. }
  64.  
  65.  
  66. private void writeLine(String str) throws IOException {
  67. fWriter.write(str+"\n");
  68. }
  69.  
  70. private void fechaEscrita() throws IOException {
  71. fWriter.close();
  72. }
  73.  
  74. }
Add Comment
Please, Sign In to add comment