Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Salvar {
- private static final String file = "db.txt";
- public void escrever(List<Pessoa> pessoas) {
- File f = new File(file);
- FileWriter fw = null;
- BufferedWriter bw = null;
- try {
- fw = new FileWriter(f);
- bw = new BufferedWriter(fw);
- for (Pessoa i : pessoas) {
- bw.write(i.getCpf() + "," + i.getNome() + "," + i.getData());
- bw.newLine();
- }
- } catch(IOException e) {
- System.err.print("Erro no acesso do arquivo.");
- }
- finally {
- try {
- fw.close();
- bw.close();
- } catch (IOException e) {
- }
- }
- }
- public List<Pessoa> ler() {
- File f = new File(file);
- FileReader fr = null;
- BufferedReader br = null;
- List<Pessoa> pessoas = new ArrayList<Pessoa>();
- try {
- fr = new FileReader(f);
- br = new BufferedReader(fr);
- String[] dados;
- for (String i = br.readLine(); i != null; i = br.readLine()) {
- dados = i.split(",");
- Pessoa temp = new Pessoa(Long.parseLong(dados[0]), dados[1], dados[2]);
- pessoas.add(temp);
- }
- return pessoas;
- } catch(IOException e) {
- System.err.print("Erro no acesso do arquivo.");
- }
- finally {
- try {
- fr.close();
- br.close();
- } catch (IOException e) {
- }
- }
- return pessoas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment