Fórum Opiniões para código criado #470820
21/02/2014
0
Sou novato em Java e ontem desenvolvi um algoritmo em Java que faz cadastro de usuários e o manda para um arquivo .txt no desktop, e além de cadastrar, esse mesmo programinha faz login com os dados que estão dentro desse .txt.
Vou postar o código aqui, ele está funcionando perfeitamente, contudo, queria a opinião de vocês sobre o que posso melhorar nele, só para fim de aprendizagem mesmo. Queria a opinião de vocês sobre como melhorar a performance dele.
DoLogin.java
package br.com.testando;
import java.io.*;
public class DoLogin {
private File f = new File("C:\\Users\\Guilherme\\Desktop\\account\\log.txt");
private FileReader fr;
private BufferedReader br ;
private String line1;
private String line2;
public DoLogin(String u, String s) throws FileNotFoundException, IOException {
getFileData();
if ( line1.equals(u) && line2.equals(s)){
System.out.println("Logado.");
} else
System.err.println("Nome de usuário ou senha inválido.");
}
private void getFileData() throws FileNotFoundException, IOException {
fr = new FileReader(f);
br = new BufferedReader(fr);
line1 = br.readLine();
line2 = br.readLine();
fr.close();
br.close();
}
}
CreateAccount.java
package br.com.testando;
import java.io.*;
public class CreateAccount {
private String dir = "C:\\Users\\Guilherme\\Desktop\\account";
private String fil = "\\log.txt";
private File directory = new File(dir);
private File file = new File(dir, fil);
FileWriter fw;
PrintWriter pw;
boolean test;
public CreateAccount(String u, String s) throws IOException, FileNotFoundException {
if(createDir() == true && createFile() == true) {
putData(u, s);
} else {
putData(u, s);
}
}
private boolean createDir() {
if((test = directory.mkdir()) == true){
return true;
} else {
return false;
}
}
private boolean createFile() throws IOException {
if ((test = file.createNewFile()) == true) {
return true;
} else {
return false;
}
}
private void putData(String u, String s) throws FileNotFoundException, IOException {
fw = new FileWriter(dir + fil, false);
pw = new PrintWriter(fw);
pw.println(u);
pw.println(s);
pw.flush();
fw.close();
pw.close();
}
}
MainModule.java
package br.com.testando;
import java.io.*;
import java.util.*;
public class MainModule {
private Scanner s;
private String st;
private CreateAccount ca;
private DoLogin dl;
public static void main(String[] args) {
MainModule mm = new MainModule();
mm.catchCommand();
try {
switch(mm.st) {
case "login":
mm.catchLogin();
break;
case "registrar":
mm.catchRegister();
break;
default:
System.out.println("Esse comando não é reconhecido.");
}
} catch(IOException e){
System.err.println("Erro no fluxo de entrada/saida de dados.");
}
}
private void catchCommand() {
s = new Scanner(System.in);
System.out.printf("> ");
st = s.nextLine();
}
private void catchLogin() throws FileNotFoundException, IOException {
String user;
String pass;
s = new Scanner(System.in);
System.out.println("Por favor, digite um login e uma senha.");
System.out.printf("> ");
user = s.nextLine();
System.out.printf("> ");
pass = s.nextLine();
dl = new DoLogin(user, pass);
}
private void catchRegister() throws IOException, FileNotFoundException {
String user;
String pass;
s = new Scanner(System.in);
System.out.println("Digite o login e a senha para cadastro.");
System.out.printf("> ");
user = s.nextLine();
System.out.printf("> ");
pass = s.nextLine();
ca = new CreateAccount(user, pass);
}
}
Guilherme Caique
Curtir tópico
+ 0Posts
21/02/2014
Eduardo Pessoa
Gostei + 0
21/02/2014
Guilherme Caique
Gostei + 0
22/02/2014
Eduardo Pessoa
[url]https://www.devmedia.com.br/criando-e-gravando-dados-em-txt-com-java/23060[/url]
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)