我需要一些關於如何使用密碼提示用戶輸入密碼的說明。我一直在爲此工作了幾天,這是爲了我的大學決賽項目,我認爲這是一個很好的Java初學者加入安全加密的步驟,因爲如果我繼續構建這個程序,我可以創建一系列的密鑰得到一個主文件。意見表示讚賞,並且我將在成功登錄前添加一個提示,詢問用戶是否確定要在某個時間點登錄。如何爲我的密鑰驗證系統添加文件上傳提示?
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner; // Needed for the Scanner class
public class TxtKeyVerifier {
public static void main(String[] args) throws FileNotFoundException {
File keyfile = new File("key2.txt");
Scanner sc = new Scanner(keyfile);
Scanner keyboard = new Scanner(System.in); //<<<---
String input;
System.out.print("Please enter your password: "); //<<---
input = sc.nextLine();
if (authenticate1(input)) {
System.out.println("This program is working if this text is found within outputfile.txt.");
File outputfile = new File("outputfile.txt");
FileOutputStream fos = new FileOutputStream(outputfile);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println("This program is working if this text is found within outputfile.txt.");
}else if (authenticate2(input)) {
System.out.println("It works.");
}else{
System.out.println("Error: Wrong password.");
}
}
private static boolean authenticate1(String password1) {
return ((password1.length() == 6)
&& (password1.matches("beep11"))
&& (password1.matches("beep11"))
&& (password1.matches("beep11")));
}
private static boolean authenticate2(String password2) {
return ((password2.length() == 6)
&& (password2.matches("beep22"))
&& (password2.matches("beep22"))
&& (password2.matches("beep22")));
}
}
不確定你在問什麼?你想有一個用戶輸入的文件路徑? –
是的,我希望用戶輸入是一個.txt文件,其中包含我的密碼。 – NumaNuma
像這樣的東西http://stackoverflow.com/questions/3309899/how-to-pass-a-text-file-as-a-argument或http://stackoverflow.com/questions/13185727/readinga-a- txt-file-using-scanner-class-in-java –