Advertisement
GuilhermeRamalho

FileWriting e FileReading

Mar 30th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package com.nanothings.files;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import javax.swing.JOptionPane;
  10.  
  11. /**
  12.  *
  13.  * @author guilherme
  14.  */
  15. public class FilesJava {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) throws IOException {
  21.         // TODO code application logic here
  22.        
  23.        
  24.         int escolha, fim=0;
  25.        
  26.         do
  27.         {
  28.             String menu = JOptionPane.showInputDialog(".::Menu de opções::.\n\n"
  29.                 + "1 - Adicionar registro\n"
  30.                 + "2 - Consular registros\n"
  31.                 + "0 - Finalizar programa\n\n");
  32.            
  33.             escolha = Integer.parseInt(menu);
  34.            
  35.             File arquivo = new File("/home/guilherme/dados.txt");
  36.            
  37.             boolean existe = arquivo.exists();
  38.            
  39.             if(existe = false)
  40.             {
  41.                 System.out.println("Novo arquivo sendo criado!");
  42.                 arquivo.createNewFile();
  43.             }
  44.            
  45.            
  46.            
  47.             if(escolha == 1)
  48.             {
  49.                 //Cadastrar
  50.                 String nome, email;
  51.                
  52.                 nome = JOptionPane.showInputDialog("Digite um nome:");
  53.                 email = JOptionPane.showInputDialog("Digite um email:");
  54.                
  55.                 try (FileWriter fw = new FileWriter(arquivo, true)) {
  56.                     BufferedWriter bw = new BufferedWriter(fw);
  57.                    
  58.                     bw.write("Nome: "+nome); bw.newLine();
  59.                     bw.write("email: "+email); bw.newLine(); bw.newLine();
  60.                    
  61.                     bw.close();
  62.                 }
  63.             }
  64.             else if(escolha == 2)
  65.             {
  66.                 //Consular
  67.                 FileReader fr = new FileReader(arquivo);
  68.                 BufferedReader br = new BufferedReader(fr);
  69.                
  70.                 while(br.ready())
  71.                 {
  72.                     String linha = br.readLine();
  73.                     System.out.println(linha);
  74.                 }
  75.         br.close();
  76.                 fr.close();
  77.             }
  78.             else if(escolha == 0)
  79.             {
  80.                 fim = 1;
  81.                 System.out.println("Programa finalizado!");
  82.             }
  83.             else
  84.             {
  85.                 JOptionPane.showMessageDialog(null, "Entrada inválida! Digite uma das seguintes opções:");
  86.             }
  87.            
  88.         }
  89.         while(fim != 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement