Bressam icon

Escrita/Leitura Arquivos

Bressam | PRO | 03/19/18 11:19:30 PM UTC | 0 ⭐ | 316 👁️ | Never ⏰ | []
Java |

2.45 KB

|

None

|

0 👍

/

0 👎

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
 
 
 
public class testeEscrita {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedWriter writer = null;
        try {
            //Criando arquivo no diretorio do projeto
            File listaUsuarios = new File(".//","ArquivoTeste.txt");
 
            // Imprime o diretorio do arquivo criado
            System.out.println(listaUsuarios.getCanonicalPath());
            
            //Escreve no arquivo
            writer = new BufferedWriter(new FileWriter(listaUsuarios,true)); //true=adiciona ao fim do arquivo
          // writer.newLine();
          // writer.write("Outra linha");
 
 
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Close the writer regardless of what happens...
                writer.close();
            } catch (Exception e) {
            }
        }
        
        // ----------- leitura arquivo
        File listaUsuarios = new File(".//","ArquivoTeste.txt");
        String linhaAtual = null;
        FileReader fr = null;
        BufferedReader reader = null;
        String idS = "";
        String saldoS = "";
        boolean lendoID=true;
            try {
                //br = new BufferedReader(new FileReader(FILENAME));
                fr = new FileReader(listaUsuarios);
                reader = new BufferedReader(fr);
                while((linhaAtual = reader.readLine())!= null)
                {
                    //System.out.println(linhaAtual);
                    for(int i=0;i<linhaAtual.length();i++)
                    {
                        char ch = linhaAtual.charAt(i);
                        //System.out.println(ch + "\n");
                        if (ch != ':' && lendoID)
                        {
                            idS = idS  + ch;
                            System.out.println(idS);
                        }
                        else
                        {
                            lendoID = false;
                            saldoS = saldoS+ch;
                        }
                        
                    }
                    System.out.println("ID:" + idS + "Saldo" + saldoS);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null)
                        reader.close();
                    if (fr != null)
                        fr.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
 
       
      //  reader.read(c);
    }
 
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎