package com.java.learing;


import java.io.BufferedReader;

import java.io.File;


/**

* @author YuFeifei

* @version 2017年11月17日 下午17:03:45

* 类说明:读取txt内容,存在map中,之后输入用户和密码模拟登陆

*/

import java.io.*;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

class ReadTxtFile{

public static Map<String,String>  readTxt(String filePath){

try{

File file = new File(filePath);//文件路径

Map<String,String>  mapTmp = new HashMap<String,String> ();//用来存储读取的用户名和密码

if(file.isFile() && file.exists()){

InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"utf-8");

BufferedReader br = new BufferedReader(isr);

String lineTxt = null;

while ((lineTxt = br.readLine()) != null){

//用split方法将用户名和密码分开

String str[] = lineTxt.split(",");

//将读取出的数据存在mapTmp中

mapTmp.put(str[0], str[1]);

}

return mapTmp;

}else{

System.out.println("文件不存在!");

}

}catch(Exception e){

System.out.println("文件读取错误!");

}

return null;

}

}


class LogIn{

public void Login(){

System.out.println("输入用户名:");

Scanner scan1 = new Scanner(System.in);

String username  = scan1.nextLine();

ReadTxtFile tmp = new ReadTxtFile();

String filePath = "D:\\User.txt";

;

if (tmp.readTxt(filePath).containsKey(username)){

System.out.println("输入密码:");

Scanner scan2 = new Scanner(System.in);

String password  = scan2.nextLine();

if(tmp.readTxt(filePath).containsValue(password) ){

System.out.println("登录成功");

}

else{

System.out.println("密码错误!");

}

}

else{

System.out.println("用户名不存在!");

}

}

}


public class TestDemo08 {

public static void main(String agrs[]){

LogIn  login = new LogIn();

login.Login();

}

}