Java基于IO流實現(xiàn)登錄和注冊功能
案例分析:
我們之前做過的登錄注冊案例是把用戶信息存進(jìn)集合里,要用IO流實現(xiàn)的話,就是要把用戶信息存入文件中。登錄注冊兩個功能的具體實現(xiàn)是在用戶操作類中,所以我們只需要在用戶操作類中把之前用戶信息存進(jìn)集合改為存進(jìn)文件就可以了。
用到的類的詳細(xì)分解:
(1)用戶類(User):用戶名,密碼,郵箱,電話號碼等等各種注冊時會用到的東西。我們在實現(xiàn)這個登錄和注冊案例的時候只需要用戶名和密碼就夠了。這是一個標(biāo)準(zhǔn)Java描述類。
package com.edu.domain;
public class User {
? ? 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 setPassWoed(String passWord) {
? ? ? ? this.passWord = passWord;
? ? }
? ? public User(String userName, String passWord) {
? ? ? ? super();
? ? ? ? this.userName = userName;
? ? ? ? this.passWord = passWord;
? ? }
? ? public User() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
}(2)我們需要定義一個接口(UserDao)來定義注冊和登錄功能。接口就是對類的功能的一種擴(kuò)展,它的本質(zhì)是用來定義規(guī)則的。
package com.edu.dao;
import java.io.IOException;
import com.edu.domain.User;
public interface UserDao {
? ? //定義兩個功能
? ? //注冊功能
? ? public abstract void regist(User user) throws IOException;
? ? //登錄功能
? ? public abstract boolean isLogin(String userName,String passWord);
}(3)用戶操作類(UserDaoImpl),也就是之前定義的接口UserDao的實現(xiàn)類。
package com.edu.dao.impl;
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 com.edu.dao.UserDao;
import com.edu.domain.User;
public class UserDaoImpl implements UserDao{
? ? public static File file = new File("user.txt");
? ? //靜態(tài)代碼塊,隨著類的加載而加載
? ? static{
? ? ? ? try{
? ? ? ? ? ? file.createNewFile();
? ? ? ? }catch(IOException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //注冊功能
? ? @Override
? ? public void regist(User user) throws IOException {
? ? ? ? try{
? ? ? ? ? ? //把用戶信息存進(jìn)文件里
? ? ? ? ? ? String info=user.getUserName()+"="+user.getPassWord();
? ? ? ? ? ? //創(chuàng)建高效字符輸出流來給文件寫入數(shù)據(jù),創(chuàng)建了一個可以追加寫入的FileWriter,避免了文件中之前的用戶信息被覆蓋
? ? ? ? ? ? BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt",true));
? ? ? ? ? ? bw.write(info);
? ? ? ? ? ? bw.newLine();
? ? ? ? ? ? bw.flush();
? ? ? ? ? ? bw.close();
? ? ? ? }catch(IOException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //登錄功能
? ? @Override
? ? public boolean isLogin(String userName, String passWord) {
? ? ? ? boolean flag=false;
? ? ? ? try{
? ? ? ? ? ? //創(chuàng)建高效字符輸入流來讀取數(shù)據(jù)
? ? ? ? ? ? BufferedReader br = new BufferedReader(new FileReader("user.txt"));
? ? ? ? ? ? String line;
? ? ? ? ? ? while ((line=br.readLine())!=null) {
? ? ? ? ? ? ? ? String[] s=line.split("=");
? ? ? ? ? ? ? ? if (s[0].equals(userName)&&s[1].equals(passWord)) {
? ? ? ? ? ? ? ? ? ? flag=true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch(IOException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return flag;
? ? }
}(4)在用戶完成注冊登錄后,我們可以實現(xiàn)某種功能了,例如:猜數(shù)字小游戲。再建一個游戲類(GuessNumber)。
package com.edu.game;
import java.util.Scanner;
public class GuessNumber {
? ? public static void playGame(){
? ? ? ? //獲取一個1到100的隨機數(shù)
? ? ? ? int random = (int)(Math.random()*100+1);
? ? ? ? //鍵盤錄入
? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? //給出提示
? ? ? ? System.out.println("請輸入所猜的數(shù)字");
? ? ? ? int number = sc.nextInt();
? ? ? ? while(true){
? ? ? ? ? ? if(number==random){
? ? ? ? ? ? ? ? System.out.println("恭喜你猜對了!");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }else if(number>random){
? ? ? ? ? ? ? ? System.out.println("大了,請繼續(xù)輸入:");
? ? ? ? ? ? ? ? number=sc.nextInt();
? ? ? ? ? ? }else if(number<random){
? ? ? ? ? ? ? ? System.out.println("小了,請繼續(xù)輸入:");
? ? ? ? ? ? ? ? number=sc.nextInt();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}(5)測試類(Test)。
package com.edu.test;
import java.io.IOException;
import java.util.Scanner;
import com.edu.dao.impl.UserDaoImpl;
import com.edu.domain.User;
import com.edu.game.GuessNumber;
public class Test {
? ? public static void main(String[] args) throws IOException {
? ? ? ? while (true) {
? ? ? ? ? ? //創(chuàng)建鍵盤錄入對象,獲取鍵盤錄入信息
? ? ? ? ? ? Scanner sc = new Scanner(System.in);
? ? ? ? ? ? System.out.println("歡迎來到注冊登錄界面!");
? ? ? ? ? ? System.out.println("1.注冊");
? ? ? ? ? ? System.out.println("2.登錄");
? ? ? ? ? ? System.out.println("3.退出");
? ? ? ? ? ? String choice = sc.nextLine();
? ? ? ? ? ? //創(chuàng)建一個用戶操作類
? ? ? ? ? ? UserDaoImpl udi = new UserDaoImpl();
? ? ? ? ? ? //利用switch循環(huán)對選擇進(jìn)行操作
? ? ? ? ? ? switch(choice){
? ? ? ? ? ? case "1":
? ? ? ? ? ? ? ? System.out.println("歡迎來到注冊界面!");
? ? ? ? ? ? ? ? System.out.println("請輸入用戶名:");
? ? ? ? ? ? ? ? String userName = sc.nextLine();
? ? ? ? ? ? ? ? System.out.println("請輸入密碼:");
? ? ? ? ? ? ? ? String passWord = sc.nextLine();
? ? ? ? ? ? ? ? User user = new User(userName,passWord);
? ? ? ? ? ? ? ? udi.regist(user);
? ? ? ? ? ? ? ? System.out.println("注冊成功!");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "2":
? ? ? ? ? ? ? ? System.out.println("歡迎來到登錄界面!");
? ? ? ? ? ? ? ? System.out.println("請輸入用戶名:");
? ? ? ? ? ? ? ? String inputUserName = sc.nextLine();
? ? ? ? ? ? ? ? System.out.println("請輸入密碼:");
? ? ? ? ? ? ? ? String InputPassWord = sc.nextLine();
? ? ? ? ? ? ? ? if (udi.isLogin(inputUserName,InputPassWord)) {
? ? ? ? ? ? ? ? ? ? System.out.println("登陸成功!可以玩游戲了!");
? ? ? ? ? ? ? ? ? ? GuessNumber.playGame();
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? System.out.println("登錄失??!");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "3":
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? //對于3或者其他數(shù)字的選擇,都直接退出系統(tǒng)
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}運行結(jié)果:
登錄的注冊的運行結(jié)果:

猜數(shù)字游戲后退出系統(tǒng):

案例實現(xiàn)過程中遇到的小問題:
如果用下面這個構(gòu)造來創(chuàng)建高效字符輸出流對象,程序每運行一次,新輸入的用戶信息會把之前的用戶信息覆蓋掉,文件中只有最近一次存入的用戶信息。
BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt");如何解決這個問題:
我們用下面這個構(gòu)造來創(chuàng)建高效字符輸出流來給文件寫入數(shù)據(jù),創(chuàng)建了一個可以追加寫入的FileWriter,避免了文件中之前的用戶信息被覆蓋。
BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt",true));參考:
FileWriter(String fileName, boolean append) //根據(jù)給定的文件名以及指示是否附加寫入數(shù)據(jù)的 boolean 值來構(gòu)造 FileWriter 對象
例如:我注冊了三次,文件中就有了這三次存入的用戶信息。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探索分析Redis?AOF日志與數(shù)據(jù)持久性
這篇文章主要為大家介紹了探索分析Redis?AOF日志與數(shù)據(jù)持久性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

