Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.geracao.main;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import javax.swing.JFileChooser;
- import javax.swing.JOptionPane;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class LeitorDeEndereços {
- public static void main(String[] args) {
- String caminho = abrirArquivo();
- // Cria um FileReader (Abre um arquivo e deixa pronto para leitura).
- FileReader fileReader = null;
- try {
- fileReader = new FileReader(caminho);
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "O arquivo não foi encontrado.");
- return;
- }
- // Lê o arquivo no fileReader usando o BufferedReader.
- BufferedReader br = new BufferedReader(fileReader);
- try {
- String linha = br.readLine();
- while (linha != null) {
- decodificar(linha);
- linha = br.readLine();
- }
- br.close();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Erro ao ler o arquivo (" + e.getLocalizedMessage() + ").");
- }
- }
- private static String abrirArquivo() {
- // Abre o componente JFileChooser.
- JFileChooser chooser = new JFileChooser();
- // Definir o diretório padrão da janela.
- chooser.setCurrentDirectory(new File("C:\\"));
- // Define um filtro de extensão, exemplo: txt.
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Arquivos TXT", "txt");
- // Aplica o filtro.
- chooser.setFileFilter(filter);
- // Abre a janela e pega o valor de resposta.
- int returnVal = chooser.showOpenDialog(null);
- if(returnVal == JFileChooser.APPROVE_OPTION) {
- // Retorna o caminho do arquivo.
- return chooser.getSelectedFile().getAbsolutePath();
- } else {
- return null;
- }
- }
- private static void decodificar(String linha) {
- String[] divisao = linha.split("#");
- String nome = divisao[0];
- int idade = Integer.parseInt(divisao[1]);
- String cidade = divisao[2];
- String estado = divisao[3];
- System.out.println("Nome: " + nome);
- System.out.println("Idade: " + idade);
- System.out.println("Cidade: " + cidade);
- System.out.println("Estado: " + estado);
- System.out.println("..................");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement