Java練習-003

來源:互聯網
上載者:User

標籤:java

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();

}

}


Java練習-003

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.