gpt4 book ai didi

java - 如何从文件中检索数据

转载 作者:行者123 更新时间:2023-12-02 13:07:08 27 4
gpt4 key购买 nike

我应该如何检索 text1 文件以用作登录信息..因为我已经在注册部分输入了详细信息..我想使用名字和员工 ID 作为登录部分的用户名和密码..p/s:我在编码方面很弱..所以请原谅我凌乱的编码:)这是我的代码。

import java.util.Scanner;
import java.io.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;


public class TestMyException12 {

static void clear() {
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}


public static void main(String[] args) {


System.out.println("1.Please register for new staff\n2.Staff login\n");
Scanner input1 = new Scanner(System.in);

FileWriter fWriter = null;
BufferedWriter writer = null;
int choice = input1.nextInt();




if (choice == 1) {
System.out.println("======================Staff Registration==================\n");
System.out.println("Please enter your personal details below:\n");

System.out.println("Enter your first name:\n");
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();

System.out.println("Enter your last name:\n");
Scanner scan1 = new Scanner(System.in);
String text1 = scan.nextLine();

System.out.println("Enter your NRIC:\n");
Scanner scan2 = new Scanner(System.in);
String text2 = scan.nextLine();

System.out.println("Enter your Staff ID:\n");
Scanner scan3 = new Scanner(System.in);
String text3 = scan.nextLine();

System.out.println("Enter your position:\n");
Scanner scan4 = new Scanner(System.in);
String text4 = scan.nextLine();

try {
fWriter = new FileWriter("text1.txt");
writer = new BufferedWriter(fWriter);
writer.write(text);
writer.write(text1);
writer.write(text2);
writer.write(text3);
writer.write(text4);
writer.newLine();
writer.close();
System.err.println("Your input data " + text.length() + " was saved.");
} catch (Exception e) {
System.out.println("Error!");
}
}
else {
System.out.println("======================Staff Login===========================");

System.out.println("\nLogin(Use your first name as username and id as password)");

System.out.println("Enter Username : ");
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();

System.out.println("Enter Password : ");
Scanner scan3 = new Scanner(System.in);
String text3 = scan.nextLine();

if (scan.equals(text) && scan.equals(text3)) {
clear();
System.out.println("Access Granted! Welcome!");
} else if (scan.equals(text)) {
System.out.println("Invalid Password!");
} else if (scan.equals(text3)) {
System.out.println("Invalid Username!");
} else {
System.out.println("Please register first!\n");
}
}
}
}

最佳答案

很简单,就像这样:

package com.coder.singleton;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class TestMyException12 {
static void clear() {
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {
}
}

public static void main(String[] args) {

System.out.println("1.Please register for new staff\n2.Staff login\n");
Scanner input1 = new Scanner(System.in);

FileWriter fWriter = null;
BufferedWriter writer = null;
int choice = input1.nextInt();
StringBuilder sb = new StringBuilder();
String text = "";

if (choice == 1) {
System.out.println("======================Staff Registration==================\n");
System.out.println("Please enter your personal details below:\n");

System.out.println("Enter your first name:\n");
text = input1.next();
sb.append(text + " ");

System.out.println("Enter your last name:\n");
text = input1.next();
sb.append(text+ " ");

System.out.println("Enter your NRIC:\n");
text = input1.next();
sb.append(text+ " ");

System.out.println("Enter your Staff ID:\n");
text = input1.next();
sb.append(text+ " ");

System.out.println("Enter your position:\n");
text = input1.next();
sb.append(text+ " ");
input1.close();
try {
fWriter = new FileWriter("text1.txt");
writer = new BufferedWriter(fWriter);
writer.write(sb.toString());
writer.newLine();
writer.close();
System.err.println("Your input data " + sb.length() + " was saved.");
} catch (Exception e) {
System.out.println("Error!");
}

}

else {

String savedName = "";
String savedPassword = "";

try {
FileInputStream fis = new FileInputStream(new File("text1.txt"));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));

String word = "";
String [] arr = null;
while ((word = bufferedReader.readLine())!= null) {

arr = word.split("\\s+");

}
savedName = arr[0];
savedPassword = arr[2];
bufferedReader.close();


} catch (Exception e) {
// TODO: handle exception
}

System.out.println("======================Staff Login===========================");

System.out.println("\nLogin(Use your first name as username and id as password)");

System.out.println("Enter Username : ");
String userName = input1.next();

System.out.println("Enter Password : ");
String password = input1.next();




if (userName.equals(savedName) && password.equals(savedPassword)) {
clear();
System.out.println("Access Granted! Welcome!");
}else if (userName.equals(savedName) && !password.equals(savedPassword)) {
System.out.println("Invalid Password!");
} else if (!userName.equals(savedName) && password.equals(savedPassword)) {
System.out.println("Invalid Username!");
} else {
System.out.println("Please register first!\n");
}

}
}
}

关于java - 如何从文件中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44099008/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com