Java中非常简单的用户登录系统

时间:2012-08-18 10:12:42

标签: java file-io java.util.scanner

我想在不使用数据库的情况下在java中创建一个简单的登录系统。(PS。没有敏感数据) 我想做这样的事情:

-1)I create a .txt file
-2)put the username and password like this

*user 
*pass 
*user
*pass
"
"

是否可以使用扫描仪执行此操作?请帮忙,因为我只是一个初学者

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用BufferedWriter来包装输出流:

public static void main(String[] args) {
    BufferedWriter writer = null;
    try {
         // create a BufferedWriter for the text file
         writer = new BufferedWriter(new FileWriter("passwords.txt"));

         // write text
         writer.write("Tudor");
         writer.newLine();
         writer.write("Tudorspassword");
         //etc.
    } catch (IOException e) {
        System.out.println("Failed to create file.");
        e.printStackTrace();
    } finally {
        if(writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                System.out.println("Failed to close file.");
                e.printStackTrace();
            }
        }
    }
}

以下是阅读方法:

public static void main(String[] args) {
    BufferedReader rdr = null;
    try {
        rdr = new BufferedReader(new FileReader("passwords.txt"));
        String name = rdr.readLine();
        String password = rdr.readLine();
        while(name != null && password != null) {
            System.out.println(name);
            System.out.println(password);               
            name = rdr.readLine();
            password = rdr.readLine();  
        }
    } catch (FileNotFoundException e) {
        System.out.println("Failed to open file.");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Failed to correctly read file.");
        e.printStackTrace();
    } finally {
        if(rdr != null) {
            try {
                rdr.close();
            } catch (IOException e) {
                System.out.println("Failed to close file.");
                e.printStackTrace();
            }
        }
    }
}

答案 1 :(得分:1)

import java.io.FileInputStream;    
import java.io.IOException;    
import java.util.Scanner;    
/**
 *
 * @author Abhishek Banerjee
 */    
public class NewMain {        
    public static void main(String[] args) throws IOException {
        Scanner s1,s2;
        s1=new Scanner(new FileInputStream("d:\\log.txt"));
        s2=new Scanner(System.in);
        boolean flag=false;
        String name,pword,n,p;
        System.out.println("Enter name:");
        n=s2.next();
        System.out.println("Enter password:");
        p=s2.next();
        while(s1.hasNext()) {
            name=s1.next();
            pword=s1.next();
            if(n.equals(name) && p.equals(pword)) {
                System.out.println("You are logged in.");
                flag=true;
                break;
            }                
        }
        if(!flag)
        System.out.println("Incorrect password.");
    }
}

答案 2 :(得分:0)

创建类User,如下所示

 class User implements Serializable 
{
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

创建可以首次序列化为文件的Hashmap实例。

 private HashMap<String, User> hashMap = new HashMap<String, User>();

使用此地图根据用户名查找密码。