viegasss Posted January 4, 2016 at 12:42 AM Report Share #591284 Posted January 4, 2016 at 12:42 AM Boas, O meu problema é o seguinte, tenho criar um ficheiro de texto onde tenho que la guardar(login e password), já tenho o código de abrir/ler/fechar ficheiro de texto... só precisava que me ajudassem em comparar com um utilizador introduz um login e uma password, ou seja ele ter que ir correr o ficheiro e ver se naquele ficheiro existe a password... =java package ficheiros; 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 java.util.Enumeration; import java.util.Vector; public class ManipulaFicheiro { // Atributos para o File (Abrir o Ficheiro) .. FileReader(para ler o fihcheiro)... private File file ; private File fileEscrita; private FileReader fr; private BufferedReader br; private FileWriter fw; private BufferedWriter bw; // Métodos... public boolean fechaFicheiro(){ if(fr !=null){ // IF PARA O FILE READER .... E FAZ OS TESTES NECESSÁRIOS.. try{ fr.close(); return true; }catch(IOException ioe){ System.out.println("Erro!"); ioe.printStackTrace(); }return false; } if(br !=null){ // IF PARA O BUFFERED READER ... E FAZ OS TESTE NECESSARIOS... try { br.close(); return true; }catch(IOException ioe){ System.out.println("Erro"); ioe.printStackTrace(); return false; } }return false; } public boolean fechaEscrita(){ if(fw !=null){ // IF PARA O FILE READER .... E FAZ OS TESTES NECESSÁRIOS.. try{ fw.close(); return true; }catch(IOException ioe){ System.out.println("Erro!"); ioe.printStackTrace(); }return false; } if(bw !=null){ // IF PARA O BUFFERED READER ... E FAZ OS TESTE NECESSARIOS... try{ bw.close(); return true; }catch(IOException ioe){ System.out.println("Erro"); ioe.printStackTrace(); return false; } }return false; } Vector<String>leFicheiro(){ Vector<String>contFicheiro=null; if (br !=null){ // IF PARA VER SE O BUFFERED READER É NULO.... contFicheiro = new Vector<String>(); // CRIAR UM VECTOR PARA PODERMOS GUARDAR A INFORMAÇÃO E MOSTRAR AO UTILIZADOR try{ String linha = null; do{ linha = br.readLine(); // VAI LER O FICHEIRO... contFicheiro.addElement(linha); // VAI CRIAR ESPAÇO SEMPRE QUE SEJA NECESSARIO... }while(linha != null); return contFicheiro; }catch(IOException ioe){ ioe.printStackTrace(); } }return null; } public boolean escreveFicheiro(Vector<String> aConteudo){ if(aConteudo !=null && aConteudo.size() > 0){ try{ String linha; Enumeration<String>lista = aConteudo.elements(); while(lista.hasMoreElements()){ linha = lista.nextElement(); bw.write(linha); // OU .. br.write(lista.nextElements()); }return true; }catch(IOException ioe){ ioe.printStackTrace(); } }return false; } public boolean abreFicheiroLeitura(String aCaminho){ // Abrir o nosso ficheiro (método) if(aCaminho !=null && aCaminho.length()>0){ // If para controlar se existe alguma coisa no ficheiro try { // Try Catch é importante para protejer o nosso ficheiro file = new File(aCaminho); // Criar o ficheiro no caminho .... if (file.exists()){ // Teste se existe alguma coisa caso exista ele cria... fr = new FileReader(file); br = new BufferedReader(fr); return true; }else{ return false; } }catch(IOException ioe){ System.out.println("O ficheiro " +aCaminho+" não está acessivel"); ioe.printStackTrace(); return false; } }else{ return false; } } public boolean abreFicheiroEscrita(String aCaminho, boolean aEstado){ if(aCaminho !=null && aCaminho.length()>0){ // If para controlar se existe alguma coisa no ficheiro try { // Try Catch é importante para protejer o nosso ficheiro fileEscrita = new File(aCaminho); fw = new FileWriter(fileEscrita, aEstado); bw = new BufferedWriter(fw); return true; }catch(IOException ioe){ System.out.println("O ficheiro " +aCaminho+" não está acessivel"); ioe.printStackTrace(); return false; } }else{ return false; } } } Link to comment Share on other sites More sharing options...
Hercles Posted January 4, 2016 at 02:17 AM Report Share #591287 Posted January 4, 2016 at 02:17 AM Como vai ser a entrada do login e password? Pela classe Scanner? Ou por uma jTextfield de um jFrame? O fixeiro armazena login e password na mesma linha? Ou em linhas diferentes? Link to comment Share on other sites More sharing options...
viegasss Posted January 4, 2016 at 03:40 AM Author Report Share #591289 Posted January 4, 2016 at 03:40 AM Nao é em ambiente gráfico! é em modo de texto! O ficheiro tem e que ficar assim na minha opiniao.... login+password login+password login+password ..... Ele tem que guardar a password e o login(para alunos, funcionario... professores e admin) e tem que ficar tudo no mesmo ficheiro.... Link to comment Share on other sites More sharing options...
Hercles Posted January 4, 2016 at 09:37 PM Report Share #591343 Posted January 4, 2016 at 09:37 PM (edited) Fiz rapidamente este código, veja se você consegue adaptar para o que você deseja. public boolean existeNoFicheiro(String login_PassWord, String caminhoFicheiro) throws FileNotFoundException { String loginSenha = login_PassWord; boolean achei = false; FileReader in = new FileReader(caminhoFicheiro); BufferedReader buf = new BufferedReader(in); try { String linha = buf.readLine(); while ((linha != null)) { if (loginSenha.equalsIgnoreCase(linha)) { achei = true; } linha = buf.readLine(); } buf.close(); } catch (Exception e) { //se der erro não faz nada } return achei; } você pode mudar lá o "equalsIgnoreCase" para "equals". Edited January 4, 2016 at 09:31 PM by Hercles Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now