Java使用IO模擬注冊(cè)登錄
本文實(shí)例為大家分享了Java使用IO模擬注冊(cè)登錄的具體代碼,供大家參考,具體內(nèi)容如下
user的pojo類
package cn.lg.pojo;
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 setPassword(String password) {
? ? ? ? this.password = password;
? ? }
? ? public User(String username, String password) {
? ? ? ? super();
? ? ? ? this.username = username;
? ? ? ? this.password = password;
? ? }
}Dao層接口
package cn.lg.dao;
import cn.lg.pojo.User;
public interface UserDao {
? ? /**
? ? ?* 注冊(cè)
? ? ?* @param uesr
? ? ?* @return
? ? ?*/
? ? boolean register(User user);
? ? /**
? ? ?* 登錄
? ? ?* @param user
? ? ?* @return
? ? ?*/
? ? boolean login(User user);
}Dao實(shí)現(xiàn)
package cn.lg.daoImpl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import cn.lg.dao.UserDao;
import cn.lg.pojo.User;
public class UserDaomImpl implements UserDao {
? ? // 使用靜態(tài)變量和靜態(tài)代碼塊,為了保證文件一加載就創(chuàng)建
? ? private static File file = new File("user.txt");
? ? static {
? ? ? ? try {
? ? ? ? ? ? file.createNewFile();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? System.out.println("創(chuàng)建文件失敗");
? ? ? ? ? ? // e.printStackTrace();
? ? ? ? }
? ? }
? ? @Override
? ? public boolean register(User user) {
? ? ? ? boolean flag = false;
? ? ? ? BufferedWriter bw = null;
? ? ? ? try {
? ? ? ? ? ? bw = new BufferedWriter(new FileWriter(file, true));// 追加
? ? ? ? ? ? bw.write(user.getUsername() + "=" + user.getPassword());
? ? ? ? ? ? bw.newLine();
? ? ? ? ? ? bw.flush();
? ? ? ? ? ? flag = true;
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? // e.printStackTrace();
? ? ? ? ? ? System.out.println("注冊(cè)失敗");
? ? ? ? } finally {
? ? ? ? ? ? if (bw != null) {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? bw.close();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? // e.printStackTrace();
? ? ? ? ? ? ? ? ? ? System.out.println("用戶注冊(cè)釋放資源失敗");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
? ? @Override
? ? public boolean login(User user) {
? ? ? ? boolean flag = false;
? ? ? ? BufferedReader br = null;
? ? ? ? try {
? ? ? ? ? ? br = new BufferedReader(new FileReader(file));
? ? ? ? ? ? String line = null;
? ? ? ? ? ? while ((line = br.readLine()) != null) {
? ? ? ? ? ? ? ? String[] datas = line.split("=");
? ? ? ? ? ? ? ? if (datas[0].equals(user.getUsername()) && datas[1].equals(user.getUsername())) {
? ? ? ? ? ? ? ? ? ? flag = true;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (FileNotFoundException e) {
? ? ? ? ? ? System.out.println("用戶登錄找不到信息所在的文件");
? ? ? ? ? ? //e.printStackTrace();
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? System.out.println("用戶登錄失敗");
? ? ? ? ? ? //e.printStackTrace();
? ? ? ? }finally {
? ? ? ? ? ? if(br!=null){
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? br.close();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? System.out.println("用戶登錄釋放資源失敗");
? ? ? ? ? ? ? ? ? ? //e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return flag;
? ? }
}控制臺(tái)模擬注冊(cè)登錄
package cn.lg.main;
import java.util.Scanner;
import cn.lg.dao.UserDao;
import cn.lg.daoImpl.UserDaomImpl;
import cn.lg.pojo.User;
/**
?* @author L
?* @date 2017年3月25日 上午11:36:31
?*
?*/
public class Main {
? ? public static void main(String[] args) {
? ? ? ? while (true) {
? ? ? ? ? ? System.out.println("-----------welcome-----------");
? ? ? ? ? ? System.out.println("1 登錄");
? ? ? ? ? ? System.out.println("2 注冊(cè)");
? ? ? ? ? ? System.out.println("3 退出");
? ? ? ? ? ? Scanner in = new Scanner(System.in);
? ? ? ? ? ? String choice = in.nextLine();
? ? ? ? ? ? // 調(diào)用Dao層
? ? ? ? ? ? UserDao userDao = new UserDaomImpl();
? ? ? ? ? ? switch (choice) {
? ? ? ? ? ? case "1":// 登錄
? ? ? ? ? ? ? ? System.out.println("------------登錄界面-----------");
? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入賬戶:");
? ? ? ? ? ? ? ? String username = in.nextLine();
? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入密碼:");
? ? ? ? ? ? ? ? String password = in.nextLine();
? ? ? ? ? ? ? ? boolean flag = userDao.login(new User(username, password));
? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? System.out.println("登錄成功");
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? System.out.println("登錄失敗");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "2":
? ? ? ? ? ? ? ? System.out.println("------------注冊(cè)界面-----------");
? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入賬戶:");
? ? ? ? ? ? ? ? String newUsername = in.nextLine();
? ? ? ? ? ? ? ? System.out.println("請(qǐng)輸入密碼:");
? ? ? ? ? ? ? ? String newPassword = in.nextLine();
? ? ? ? ? ? ? ? userDao.register(new User(newUsername, newPassword));
? ? ? ? ? ? ? ? System.out.println("注冊(cè)成功");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case "3":
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? System.out.println("謝謝使用,歡迎下次再來");
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java+mysql實(shí)現(xiàn)登錄和注冊(cè)功能
- Java實(shí)現(xiàn)登錄和注冊(cè)案例
- Java基于IO流實(shí)現(xiàn)登錄和注冊(cè)功能
- Java實(shí)現(xiàn)登錄與注冊(cè)頁面
- javaweb實(shí)現(xiàn)注冊(cè)登錄頁面
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Java+mysql用戶注冊(cè)登錄功能
- JAVA簡單實(shí)現(xiàn)MD5注冊(cè)登錄加密實(shí)例代碼
- Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
- java實(shí)現(xiàn)注冊(cè)登錄系統(tǒng)
相關(guān)文章
解決java 查看JDK中底層源碼的實(shí)現(xiàn)方法
本篇文章是對(duì)在java中查看JDK中底層源碼的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問題
下面小編就為大家?guī)硪黄昝澜鉀QEclipse 項(xiàng)目有紅感嘆號(hào)的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Java多線程執(zhí)行處理業(yè)務(wù)時(shí)間太久解決方法代碼示例
這篇文章主要介紹了Java多線程執(zhí)行處理業(yè)務(wù)時(shí)間太久解決方法代碼示例的相關(guān)資料,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
使用javaMail實(shí)現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了使用javaMail實(shí)現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對(duì)SpringCloud?Ribbon負(fù)載均衡相關(guān)知識(shí)感興趣的朋友一起看看吧2022-06-06
Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
這篇文章主要為大家介紹了如何利用Java語言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05
SpringBoot中SmartLifecycle的使用解析
這篇文章主要介紹了SpringBoot中SmartLifecycle的使用解析,SmartLifecycle是一個(gè)擴(kuò)展了Lifecycle接口,可以跟蹤spring容器ApplicationContext刷新或者關(guān)閉的接口,實(shí)現(xiàn)該接口的實(shí)現(xiàn)類有特定的執(zhí)行順序,需要的朋友可以參考下2023-11-11
Java中如何取出String字符串括號(hào)中的內(nèi)容
這篇文章主要介紹了Java中如何取出String字符串括號(hào)中的內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

