Advertisement
gastaojunior

Botao Exportar para arquivo

Mar 26th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1.     if (!ListaDePessoasImportadas.isEmpty()) {
  2.             PosicaoNaListaDePessoasImportadas = 0;
  3.            
  4.             JFileChooser f = new JFileChooser();
  5.             f.setDialogTitle("Entre com o nome de arquivo");
  6.            
  7.             f.setFileFilter(new javax.swing.filechooser.FileFilter() {
  8.                 //Filtro, converte as letras em minúsculas antes de comparar
  9.  
  10.                 public boolean accept(File ff) {
  11.                     return ff.getName().toLowerCase().endsWith(".txt") || ff.isDirectory();
  12.                 }
  13.                 //Texto que será exibido para o usuário
  14.  
  15.                 public String getDescription() {
  16.                     return "Arquivo de texto (.txt)";
  17.                 }
  18.             });
  19.            
  20.            
  21.             int returnVal = f.showOpenDialog(this);
  22.            
  23.             if (returnVal == JFileChooser.APPROVE_OPTION) {
  24.                 File arquivo = f.getSelectedFile();
  25.                 try {
  26.                     FileWriter writer = new FileWriter(arquivo);
  27.                     PrintWriter saida = new PrintWriter(writer, true);
  28.                    
  29.                     String linha = "";
  30.                     Pessoa pes;
  31.                     for (int i = 0; i <= ListaDePessoasImportadas.size() - 1; i++) {
  32.                         pes = ListaDePessoasImportadas.get(i);
  33.                         linha = pes.getNome() + ";" + pes.getTelefone();
  34.                         saida.println(linha);
  35.                     }
  36.                     writer.close();
  37.                     JOptionPane.showMessageDialog(this, "Exportação concluída com sucesso");
  38.                 } catch (Exception e) {
  39.                     JOptionPane.showMessageDialog(this, "Problemas no descritor do arquivo:" + e.getMessage());
  40.                 }
  41.             } else {
  42.                 JOptionPane.showMessageDialog(this, "Comando cancelado");
  43.             }
  44.         } else {
  45.             JOptionPane.showMessageDialog(this, "A lista de pessoas para exportar está vazia.");
  46.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement