Java Exercise-003

Source: Internet
Author: User





* @author Yufeifei

* @version November 17, 2017 17:03:45

* Class Description: Read txt content, exist in map, then enter user and password simulation login



Import Java.util.HashMap;

Import Java.util.Map;

Import Java.util.Scanner;

Class readtxtfile{

public static map<string,string> Readtxt (String filePath) {


File File = new (FilePath);//Files path

map<string,string> maptmp = new hashmap<string,string> ();//used to store the read user name and password

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

Separate username and password with split method

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

The data that is being read out exists in maptmp

Maptmp.put (Str[0], str[1]);


return maptmp;


SYSTEM.OUT.PRINTLN ("file does not exist!) ");


}catch (Exception e) {

SYSTEM.OUT.PRINTLN ("File read error! ");


return null;



Class login{

public void Login () {

System.out.println ("Enter user name:");

Scanner scan1 = new Scanner (;

String username = Scan1.nextline ();

Readtxtfile tmp = new Readtxtfile ();

String FilePath = "D:\\user.txt";


if (Tmp.readtxt (FilePath). ContainsKey (username)) {

System.out.println ("Enter password:");

Scanner scan2 = new Scanner (;

String password = scan2.nextline ();

if (Tmp.readtxt (FilePath). Containsvalue (password)) {

SYSTEM.OUT.PRINTLN ("Login Successful");



SYSTEM.OUT.PRINTLN ("Bad password!") ");




System.out.println ("User name does not exist! ");




public class TestDemo08 {

public static void Main (String agrs[]) {

Login login = new login ();

Login. Login ();



Java Exercise-003

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.