2016-12-01 8 views
0

私は、パスワードを含むキーファイルの入力を求めるプロンプトを出す方法を教えてください。私は数日間この作業に取り組んできました。それは私の大学ファイナルプロジェクトのためのものです。私はこの1つのプログラムを構築し続けると、一連のキーを作成することができたので、 1つのメインファイルに到達する。アイデアは高く評価されました。ユーザーがある時点でログインしたいと思っているかどうかを尋ねる、ログインに成功する前にプロンプ​​トを追加します。鍵照合システムのファイルアップロードのプロンプトを追加するにはどうすればよいですか?

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"))); 
} 
} 
+0

あなたは何を求めているのですか?ユーザーはファイルパスを入力する必要がありますか? –

+0

はい、ユーザー入力はパスワードが入った.txtファイルにします。 – NumaNuma

+0

このようなものhttp://stackoverflow.com/questions/3309899/how-to-pass-a-text-file-as-a-argumentまたはhttp://stackoverflow.com/questions/13185727/reading-a- txtファイルを使用するスキャナクラスのJava –

答えて

0

スキャナのファイルを読むには、ユーザーの入力を渡す必要があります。私はあなたのコードのいくつかの行をこれを行うために微調整しました。これが役立つかどうかを見てください。

public class TxtKeyVerifier { 

    public static void main(String[] args) throws FileNotFoundException { 

    Scanner keyboard = new Scanner(System.in); 
    JFileChooser fileChooser = new JFileChooser("."); 
    fileChooser.showOpenDialog(null); 
    File keyfile = fileChooser.getSelectedFile(); 

     Scanner sc = new Scanner(keyfile); 
     String 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."); 
     } 
     sc.close(); 
     keyboard.close(); 
    } 

    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"))); 
    } 
} 
+0

偉大な答え、私はあなたの編集の前にAnacronの答えが本当に好きだった。実際に暗号化されたログインシステムを設計するには、タイプされたキーファイルパス経由のログインがかなり良いスタートです。 – NumaNuma

+0

母私はあなたのコメントの1つにこれを載せているのを見ました: 'ユーザーに "ファイルを選んでください"というウィンドウが表示されました。だから、私は答えを編集しました。 __:)__私は以前の回答に戻しますか? – anacron

0

あなたはスイングパッケージからFileChooserこの場合JFileChooserを使用したいです。ドキュメントから

JFileChooserはユーザーがファイルを選択する単純な機構を提供します。 JFileChooserの使い方については、「Javaチュートリアル」の「ファイルセレクタを使用する方法」を参照してください。 FileChooserパスを取得すると

JFileChooser chooser = new JFileChooser(); 
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif"); 
chooser.setFileFilter(filter); 
int returnVal = chooser.showOpenDialog(parent); 
if(returnVal == JFileChooser.APPROVE_OPTION) { 
    System.out.println("You chose to open this file: " + 
     chooser.getSelectedFile().getName()); 
} 

: 次のコードは、唯一.JPGや画像.gifメモ見たユーザのホームディレクトリのファイルチューをポップアップ表示されます。ファイルのパスをスキャナに渡し、既存のコードを実行します。技術的な可能性に関する完全なガイドは、Oracle: How to Use File Choosers

希望です。

+0

ありがとうMurat K.それは助けてくれた! – NumaNuma

関連する問題