Bressam

Escrita/Leitura Arquivos

Mar 19th, 2018
350
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.nio.file.Paths;
  9.  
  10.  
  11.  
  12. public class testeEscrita {
  13.  
  14.     /**
  15.      * @param args
  16.      */
  17.     public static void main(String[] args) {
  18.         // TODO Auto-generated method stub
  19.         BufferedWriter writer = null;
  20.         try {
  21.             //Criando arquivo no diretorio do projeto
  22.             File listaUsuarios = new File(".//","ArquivoTeste.txt");
  23.  
  24.             // Imprime o diretorio do arquivo criado
  25.             System.out.println(listaUsuarios.getCanonicalPath());
  26.            
  27.             //Escreve no arquivo
  28.             writer = new BufferedWriter(new FileWriter(listaUsuarios,true)); //true=adiciona ao fim do arquivo
  29.           // writer.newLine();
  30.           // writer.write("Outra linha");
  31.  
  32.  
  33.            
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         } finally {
  37.             try {
  38.                 // Close the writer regardless of what happens...
  39.                 writer.close();
  40.             } catch (Exception e) {
  41.             }
  42.         }
  43.        
  44.         // ----------- leitura arquivo
  45.         File listaUsuarios = new File(".//","ArquivoTeste.txt");
  46.         String linhaAtual = null;
  47.         FileReader fr = null;
  48.         BufferedReader reader = null;
  49.         String idS = "";
  50.         String saldoS = "";
  51.         boolean lendoID=true;
  52.             try {
  53.                 //br = new BufferedReader(new FileReader(FILENAME));
  54.                 fr = new FileReader(listaUsuarios);
  55.                 reader = new BufferedReader(fr);
  56.                 while((linhaAtual = reader.readLine())!= null)
  57.                 {
  58.                     //System.out.println(linhaAtual);
  59.                     for(int i=0;i<linhaAtual.length();i++)
  60.                     {
  61.                         char ch = linhaAtual.charAt(i);
  62.                         //System.out.println(ch + "\n");
  63.                         if (ch != ':' && lendoID)
  64.                         {
  65.                             idS = idS  + ch;
  66.                             System.out.println(idS);
  67.                         }
  68.                         else
  69.                         {
  70.                             lendoID = false;
  71.                             saldoS = saldoS+ch;
  72.                         }
  73.                        
  74.                     }
  75.                     System.out.println("ID:" + idS + "Saldo" + saldoS);
  76.                 }
  77.             } catch (IOException e) {
  78.                 // TODO Auto-generated catch block
  79.                 e.printStackTrace();
  80.             } finally {
  81.                 try {
  82.                     if (reader != null)
  83.                         reader.close();
  84.                     if (fr != null)
  85.                         fr.close();
  86.                 } catch (IOException ex) {
  87.                     ex.printStackTrace();
  88.                 }
  89.             }
  90.  
  91.        
  92.       //  reader.read(c);
  93.     }
  94.  
  95. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment