This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
java:console_read [2015/06/19 16:12] gthanos created |
java:console_read [2016/02/26 11:15] (current) |
||
|---|---|---|---|
| Line 7: | Line 7: | ||
| ===== Διαβάζοντας από το System.in ===== | ===== Διαβάζοντας από το System.in ===== | ||
| - | ===== Διαβάζοντας με χρήση της κλάσης Console ===== | + | To **System.in** είναι ένα stream τύπου byte-stream. Συνήθως από την κονσόλα θέλουμε να διαβάσουμε χαρακτήρες. Στις περιπτώσεις αυτές απαιτείται να το μετατρέψουμε σε character stream ως εξής: |
| + | |||
| + | <code java> | ||
| + | InputStreamReader cin = new InputStreamReader(System.in); | ||
| + | </code> | ||
| + | |||
| + | Τον παραπάνω κώδικα συνήθως τον εντάσσουμε μέσα σε ένα block try/catch, όπως παρακάτω | ||
| + | |||
| + | <code java> | ||
| + | try (InputStreamReader r = new InputStreamReader(System.in)) { | ||
| + | char []buf = new char[128]; | ||
| + | String word = null; | ||
| + | int len = 0; | ||
| + | System.out.print("Enter your input: "); | ||
| + | if((len = r.read(buf,0,128)) != -1) { | ||
| + | word = new String(buf); | ||
| + | word = word.substring(0, len-1); | ||
| + | System.out.println("Your input is: "+word); | ||
| + | } | ||
| + | else { | ||
| + | System.out.println("No input!"); | ||
| + | } | ||
| + | } catch (IOException ex) { | ||
| + | System.out.println("Unable to read from STDIN"); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Για την διευκόλυνση σας, κατά την ανάγνωση μπορείτε να δημιουργήσετε ένα αντικείμενο της κλάσης java.io.BufferedReader, όπως παρακάτω. Το πλεονέκτημα της συγκεκριμένης επιλογής είναι ότι η κλάση BufferedReader σας δίνει την δυνατότητα να διαβάσετε απευθείας μία γραμμή από το stream μέσω της μεθόδου readLine, την οποία επιστρέφει ως String. | ||
| + | |||
| + | <code java> | ||
| + | try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in)) ) { | ||
| + | String word = null; | ||
| + | System.out.print("Enter your input: "); | ||
| + | if( (word=r.readLine()) != null) { | ||
| + | System.out.println("Your input is: "+word); | ||
| + | } | ||
| + | else { | ||
| + | System.out.println("No input!"); | ||
| + | } | ||
| + | }catch (IOException ex) { | ||
| + | System.out.println("Unable to read from STDIN"); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | <WRAP info 80% center round> | ||
| + | Σε όλες τις περιπτώσεις που ανοίγετε ένα stream υποχρεούστε να ελέγχετε για πιθανή δημιουργία IOException κατά το άνοιγμα του stream. Ο compiler της Java θα εμφανίσει λάθος σε περίπτωση που δεν το κάνετε. | ||
| + | </WRAP> | ||
| + | |||
| + | ===== Διαβάζοντας με χρήση της κλάσης java.io.Console ===== | ||
| + | |||
| + | Μπορείτε να διαβάσετε ή να γράψετε στην κονσόλα με την βοήθεια της κλάσης [[http://docs.oracle.com/javase/7/docs/api/java/io/Console.html|java.io.Console]]. Το παρακάτω παράδειγμα είναι από το site της Oracle και δείχνει την χρήση της κλάσης μέσω των μεθόδων **readLine** και **readPassword**. | ||
| + | |||
| + | <code java Password.java> | ||
| + | import java.io.Console; | ||
| + | import java.util.Arrays; | ||
| + | import java.io.IOException; | ||
| + | |||
| + | public class Password { | ||
| + | |||
| + | public static void main (String args[]) throws IOException { | ||
| + | |||
| + | Console c = System.console(); | ||
| + | if (c == null) { | ||
| + | System.err.println("No console."); | ||
| + | System.exit(1); | ||
| + | } | ||
| + | |||
| + | String login = c.readLine("Enter your login: "); | ||
| + | char [] oldPassword = c.readPassword("Enter your old password: "); | ||
| + | |||
| + | if (verify(login, oldPassword)) { | ||
| + | boolean noMatch; | ||
| + | do { | ||
| + | char [] newPassword1 = c.readPassword("Enter your new password: "); | ||
| + | char [] newPassword2 = c.readPassword("Enter new password again: "); | ||
| + | noMatch = ! Arrays.equals(newPassword1, newPassword2); | ||
| + | if (noMatch) { | ||
| + | c.format("Passwords don't match. Try again.%n"); | ||
| + | } else { | ||
| + | change(login, newPassword1); | ||
| + | c.format("Password for %s changed.%n", login); | ||
| + | } | ||
| + | Arrays.fill(newPassword1, ' '); | ||
| + | Arrays.fill(newPassword2, ' '); | ||
| + | } while (noMatch); | ||
| + | } | ||
| + | |||
| + | Arrays.fill(oldPassword, ' '); | ||
| + | } | ||
| + | |||
| + | // Dummy change method. | ||
| + | static boolean verify(String login, char[] password) { | ||
| + | // This method always returns | ||
| + | // true in this example. | ||
| + | // Modify this method to verify | ||
| + | // password according to your rules. | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | // Dummy change method. | ||
| + | static void change(String login, char[] password) { | ||
| + | // Modify this method to change | ||
| + | // password according to your rules. | ||
| + | } | ||
| + | } | ||
| + | </code> | ||