Advertisement
Guest User

Métodos Ficheiros Texto

a guest
Dec 1st, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. public void abreLeitura(String nomeDoFicheiro) throws IOException {
  2.     fR = new BufferedReader(new FileReader(nomeDoFicheiro));
  3. }
  4.  
  5. public void abreEscrita(String nomeDoFicheiro) throws IOException {
  6.     fW = new BufferedWriter(new FileWriter(nomeDoFicheiro));
  7. }
  8.  
  9. //Método para ler uma linha do ficheiro
  10. //Devolve a linha lida
  11. public String leLinha() throws IOException {
  12.     return fR.readLine();
  13. }
  14.  
  15. //Método para ler um número do ficheiro
  16. //Devolve o número lido
  17. public int[] leNumeroInt() throws IOException {
  18.     int[] result = new int[2];
  19.     String st = fR.readLine();
  20.     if (st != null) {
  21.         result[0] = 0;
  22.         result[1] = Integer.parseInt(st);
  23.     } else {
  24.         result[0] = -1;
  25.     }
  26.    
  27.     return result;
  28. }
  29.  
  30. //Método para escrever uma linha no ficheiro
  31. //Recebe a linha a escrever
  32. public void escreveLinha(String linha) throws IOException {
  33.     fW.write(linha,0,linha.length());
  34.     fW.newLine();
  35. }
  36.  
  37. //Método para escrever um número inteiro no ficheiro
  38. //Recebe o número a escrever
  39. public void escreveNumero(int num) throws IOException {
  40.     String st = "";
  41.     st = st.valueOf(num);
  42.     escreveLinha(st);
  43. }
  44.  
  45. //Método para fechar um ficheiro aberto em modo leitura
  46. public void fechaLeitura() throws IOException {
  47.     fR.close();
  48. }
  49.  
  50. //Método para fechar um ficheiro aberto em modo escrita
  51. public void fechaEscrita() throws IOException {
  52.     fW.close();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement