Advertisement
vinedfs

LeitorDeEndereços.java

Jul 23rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package br.com.geracao.main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7.  
  8. import javax.swing.JFileChooser;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.filechooser.FileNameExtensionFilter;
  11.  
  12. public class LeitorDeEndereços {
  13.  
  14.     public static void main(String[] args) {
  15.        
  16.        
  17.         String caminho = abrirArquivo();
  18.        
  19.         // Cria um FileReader (Abre um arquivo e deixa pronto para leitura).
  20.         FileReader fileReader = null;
  21.         try {
  22.             fileReader = new FileReader(caminho);
  23.         } catch (FileNotFoundException e) {
  24.             JOptionPane.showMessageDialog(null, "O arquivo não foi encontrado.");
  25.             return;
  26.         }
  27.        
  28.         // Lê o arquivo no fileReader usando o BufferedReader.
  29.         BufferedReader br = new BufferedReader(fileReader);
  30.         try {
  31.             String linha = br.readLine();
  32.             while (linha != null) {
  33.                 decodificar(linha);
  34.                 linha = br.readLine();
  35.             }
  36.             br.close();
  37.         } catch (Exception e) {
  38.             JOptionPane.showMessageDialog(null, "Erro ao ler o arquivo (" + e.getLocalizedMessage() + ").");
  39.         }
  40.        
  41.     }
  42.  
  43.     private static String abrirArquivo() {
  44.         // Abre o componente JFileChooser.
  45.         JFileChooser chooser = new JFileChooser();
  46.         // Definir o diretório padrão da janela.
  47.         chooser.setCurrentDirectory(new File("C:\\"));
  48.         // Define um filtro de extensão, exemplo: txt.
  49.         FileNameExtensionFilter filter = new FileNameExtensionFilter("Arquivos TXT", "txt");
  50.         // Aplica o filtro.
  51.         chooser.setFileFilter(filter);
  52.         // Abre a janela e pega o valor de resposta.
  53.         int returnVal = chooser.showOpenDialog(null);
  54.         if(returnVal == JFileChooser.APPROVE_OPTION) {
  55.             // Retorna o caminho do arquivo.
  56.             return chooser.getSelectedFile().getAbsolutePath();
  57.         } else {
  58.             return null;
  59.         }
  60.     }
  61.    
  62.     private static void decodificar(String linha) {
  63.        
  64.         String[] divisao = linha.split("#");
  65.         String nome = divisao[0];
  66.         int idade = Integer.parseInt(divisao[1]);
  67.         String cidade = divisao[2];
  68.         String estado = divisao[3];
  69.        
  70.         System.out.println("Nome: " + nome);
  71.         System.out.println("Idade: " + idade);
  72.         System.out.println("Cidade: " + cidade);
  73.         System.out.println("Estado: " + estado);
  74.         System.out.println("..................");
  75.        
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement