Guest User

ManipulaArquivo

a guest
May 13th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. public class ManipulaArquivo {
  2.  
  3.     public ManipulaArquivo() {
  4.     }
  5.  
  6.     public List<String> abrirArquivo(String caminho) {
  7.         List<String> texto = new ArrayList<>();
  8.         File arq = new File(caminho);
  9.         if (arq.exists()) {
  10.             try {
  11.                 //OpenFile
  12.                 FileReader arquivo = new FileReader(caminho);
  13.                 BufferedReader conteudoDoArquivo = new BufferedReader(arquivo);
  14.                 String linha = conteudoDoArquivo.readLine();
  15.                 while (linha != null) {
  16.                     texto.add(linha);
  17.                     linha = conteudoDoArquivo.readLine();
  18.                 }
  19.                 conteudoDoArquivo.close();
  20.             } catch (Exception e) {//Catch exception if any
  21.                 System.err.println("Error: " + e.getMessage());
  22.             }
  23.         } else {
  24.             texto.add("");
  25.         }
  26.         return texto;
  27.     }
  28.  
  29.     public int salvarArquivo(String caminho, List<String> texto) {
  30.         try {
  31.             // Create file
  32.             FileWriter arquivo = new FileWriter(caminho);
  33.             BufferedWriter conteudoDoArquivo = new BufferedWriter(arquivo);
  34.             for (int i = 0; i < texto.size(); i++) {
  35.                 conteudoDoArquivo.write(texto.get(i) + "\n");//+ System.getProperty("line.separator")); //
  36.             }
  37.             conteudoDoArquivo.close();
  38.         } catch (Exception e) {//Catch exception if any
  39.             System.err.println("Error: " + e.getMessage());
  40.             return 1; //houve erro
  41.         }
  42.         return 0;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment