输入输出缓冲流(注册登录实现)

package cn.lyjs.buffered;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Login {
    static Scanner scanner=new Scanner(System.in);
    
    public static void main(String[] args) throws IOException {
        while(true){
            System.out.println("注册请输入:A  登录请输入 :B");
            String ch=scanner.next();
            if(ch.equalsIgnoreCase("A")){
                Reg();
            }else if(ch.equalsIgnoreCase("B")){
                Login();
            }else{
                System.out.println("你输入有误,请重新输入:");
            }
        }
    }
    
    public static void Login() throws IOException{
        System.out.println("请输入用户名:");
        String username=scanner.next();
        System.out.println("请输入密码:");
        String password=scanner.next();
        String info=username+" "+password;
        File file=new File("E:\\user.txt");
        FileReader fileReader=new FileReader(file);
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        boolean isTrue=false;
        String line=null;
        while((line=bufferedReader.readLine())!=null){
            if(line.equals(info)){
                isTrue=true;
            }
        }
        if(isTrue){
            System.out.println("欢迎你成功登录");
        }else{
            System.out.println("用户名或密码错误,请重新登录");
        }
        
    }
    public static void Reg() throws IOException{
        System.out.println("请输入用户名:");
        String username=scanner.next();
        System.out.println("请输入密码:");
        String password=scanner.next();
        String info=username+" "+password;
        File file=new File("E:\\user.txt");
        FileWriter fileWriter=new FileWriter(file,true);
        BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
        bufferedWriter.append(info);
        bufferedWriter.newLine();        
        bufferedWriter.close();
        System.out.println("你已经注册成功");
    }
}

 

posted @ 2015-11-28 15:59  好人难寻  阅读(254)  评论(0编辑  收藏  举报