我應該如何檢索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");
}
}
}
}