Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nanothings.files;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import javax.swing.JOptionPane;
- /**
- *
- * @author guilherme
- */
- public class FilesJava {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws IOException {
- // TODO code application logic here
- int escolha, fim=0;
- do
- {
- String menu = JOptionPane.showInputDialog(".::Menu de opções::.\n\n"
- + "1 - Adicionar registro\n"
- + "2 - Consular registros\n"
- + "0 - Finalizar programa\n\n");
- escolha = Integer.parseInt(menu);
- File arquivo = new File("/home/guilherme/dados.txt");
- boolean existe = arquivo.exists();
- if(existe = false)
- {
- System.out.println("Novo arquivo sendo criado!");
- arquivo.createNewFile();
- }
- if(escolha == 1)
- {
- //Cadastrar
- String nome, email;
- nome = JOptionPane.showInputDialog("Digite um nome:");
- email = JOptionPane.showInputDialog("Digite um email:");
- try (FileWriter fw = new FileWriter(arquivo, true)) {
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write("Nome: "+nome); bw.newLine();
- bw.write("email: "+email); bw.newLine(); bw.newLine();
- bw.close();
- }
- }
- else if(escolha == 2)
- {
- //Consular
- FileReader fr = new FileReader(arquivo);
- BufferedReader br = new BufferedReader(fr);
- while(br.ready())
- {
- String linha = br.readLine();
- System.out.println(linha);
- }
- br.close();
- fr.close();
- }
- else if(escolha == 0)
- {
- fim = 1;
- System.out.println("Programa finalizado!");
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Entrada inválida! Digite uma das seguintes opções:");
- }
- }
- while(fim != 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement