欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用IO模擬注冊(cè)登錄

 更新時(shí)間:2022年04月25日 09:32:34   作者:_路人_  
這篇文章主要為大家詳細(xì)介紹了Java使用IO模擬注冊(cè)登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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("謝謝使用,歡迎下次再來(lái)");
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決java 查看JDK中底層源碼的實(shí)現(xiàn)方法

    解決java 查看JDK中底層源碼的實(shí)現(xiàn)方法

    本篇文章是對(duì)在java中查看JDK中底層源碼的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問(wèn)題

    完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問(wèn)題

    下面小編就為大家?guī)?lái)一篇完美解決Eclipse 項(xiàng)目有紅感嘆號(hào)的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Java多線程執(zhí)行處理業(yè)務(wù)時(shí)間太久解決方法代碼示例

    Java多線程執(zhí)行處理業(yè)務(wù)時(shí)間太久解決方法代碼示例

    這篇文章主要介紹了Java多線程執(zhí)行處理業(yè)務(wù)時(shí)間太久解決方法代碼示例的相關(guān)資料,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 使用javaMail實(shí)現(xiàn)發(fā)送郵件

    使用javaMail實(shí)現(xiàn)發(fā)送郵件

    這篇文章主要為大家詳細(xì)介紹了使用javaMail實(shí)現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼

    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基礎(chǔ)教程之字符流文件讀寫

    Java基礎(chǔ)教程之字符流文件讀寫

    這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之字符流文件讀寫的相關(guān)資料,,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • SpringBoot的啟動(dòng)速度優(yōu)化

    SpringBoot的啟動(dòng)速度優(yōu)化

    隨著我們項(xiàng)目的不斷迭代 Bean 的數(shù)量會(huì)大大增加,如果都在啟動(dòng)時(shí)進(jìn)行初始化會(huì)非常耗時(shí),本文主要介紹了SpringBoot的啟動(dòng)速度優(yōu)化,感興趣的可以了解一下
    2023-09-09
  • Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼

    Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼

    這篇文章主要為大家介紹了如何利用Java語(yǔ)言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-05-05
  • SpringBoot中SmartLifecycle的使用解析

    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)容

    這篇文章主要介紹了Java中如何取出String字符串括號(hào)中的內(nèi)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評(píng)論