Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
Advertisement