java:console_read
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:console_read [2015/06/20 01:35] – [Διαβάζοντας από το System.in] gthanos | java:console_read [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 7: | Line 7: | ||
===== Διαβάζοντας από το System.in ===== | ===== Διαβάζοντας από το System.in ===== | ||
- | To **System.in** είναι ένα stream τύπου byte-stream. Συνήθως από την κονσόλα θέλουμε να διαβάσουμε χαρακτήρες, για τον λόγο | + | To **System.in** είναι ένα stream τύπου byte-stream. Συνήθως από την κονσόλα θέλουμε να διαβάσουμε χαρακτήρες. Στις περιπτώσεις |
<code java> | <code java> | ||
Line 13: | Line 13: | ||
</ | </ | ||
- | Για την διευκόλυνση σας μπορείτε να δημιουργήσετε ένα BufferedReader προκειμένου να μπορείτε να διαβάζετε | + | Τον παραπάνω κώδικα συνήθως τον εντάσσουμε μέσα σε ένα block try/catch, όπως παρακάτω |
+ | |||
+ | <code java> | ||
+ | try (InputStreamReader r = new InputStreamReader(System.in)) { | ||
+ | char []buf = new char[128]; | ||
+ | | ||
+ | int len = 0; | ||
+ | | ||
+ | | ||
+ | word = new String(buf); | ||
+ | word = word.substring(0, | ||
+ | | ||
+ | } | ||
+ | else { | ||
+ | | ||
+ | } | ||
+ | } catch (IOException ex) { | ||
+ | System.out.println(" | ||
+ | System.exit(1); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Για την διευκόλυνση σας, κατά την ανάγνωση | ||
<code java> | <code java> | ||
try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in)) ) { | try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in)) ) { | ||
- | | + | String word = null; |
+ | System.out.print(" | ||
+ | if( (word=r.readLine()) != null) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | else { | ||
+ | System.out.println(" | ||
+ | } | ||
}catch (IOException ex) { | }catch (IOException ex) { | ||
System.out.println(" | System.out.println(" | ||
Line 24: | Line 53: | ||
</ | </ | ||
+ | <WRAP info 80% center round> | ||
+ | Σε όλες τις περιπτώσεις που ανοίγετε ένα stream υποχρεούστε να ελέγχετε για πιθανή δημιουργία IOException κατά το άνοιγμα του stream. Ο compiler της Java θα εμφανίσει λάθος σε περίπτωση που δεν το κάνετε. | ||
+ | </ | ||
- | ===== Διαβάζοντας με χρήση της κλάσης Console ===== | + | ===== Διαβάζοντας με χρήση της κλάσης |
+ | |||
+ | Μπορείτε να διαβάσετε ή να γράψετε στην κονσόλα με την βοήθεια της κλάσης [[http:// | ||
+ | |||
+ | <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(" | ||
+ | System.exit(1); | ||
+ | } | ||
+ | |||
+ | String login = c.readLine(" | ||
+ | char [] oldPassword = c.readPassword(" | ||
+ | |||
+ | if (verify(login, | ||
+ | boolean noMatch; | ||
+ | do { | ||
+ | char [] newPassword1 = c.readPassword(" | ||
+ | char [] newPassword2 = c.readPassword(" | ||
+ | noMatch = ! Arrays.equals(newPassword1, | ||
+ | if (noMatch) { | ||
+ | c.format(" | ||
+ | } else { | ||
+ | change(login, | ||
+ | c.format(" | ||
+ | } | ||
+ | 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. | ||
+ | } | ||
+ | } | ||
+ | </ | ||
java/console_read.1434764124.txt.gz · Last modified: 2015/06/20 00:35 (external edit)