Java - 標準入出力


■ページ目次

Top

■標準入出力

■OS の機能
Top

■バイト単位の標準入力

■例題
  1. 標準入力から入力した 1 バイトずつのデータを標準出力に出力するアプリケーション"Sysin2Sysout.java"を作成ください。入力の終了は"Ctrl+C"キーの押下によります。
    1. import java.io.*;
    2. public class Sysin2Sysout {
    3. public static void main (String[] args) {
    4. int b;
    5. try {
    6. while ((b = System.in.read()) != -1) {
    7. System.out.println("char = " + (char)b + ", hex = " +
    8. Integer.toHexString(b));
    9. }
    10. } catch (IOException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. }

    □ 実行結果

    $ java Sysin2Sysout
    Hello                  <-- 入力したデータ  
    char = H, hex = 48
    char = e, hex = 65
    char = l, hex = 6c
    char = l, hex = 6c
    char = o, hex = 6f
    , hex = d
    char =
    , hex = a
    あいうえお             <-- 入力したデータ  
    char = ?, hex = 82
    char = ?, hex = a0
    char = ?, hex = 82
    char = ¢, hex = a2
    char = ?, hex = 82
    char = ?, hex = a4
    char = ?, hex = 82
    char = ?, hex = a6
    char = ?, hex = 82
    char = ¨, hex = a8
    , hex = d
    char =
    , hex = a
    
Top

■行単位の標準入力

■例題
  1. 標準入力からの入力に、日本語が使用できるようにするアプリケーション"Sysin2SysoutWithJp.java"を作成ください。入力の終了は"Ctrl+C"キーの押下によります。
    1. import java.io.*;
    2. public class Sysin2SysoutWithJp {
    3. public static void main (String[] args) {
    4. BufferedReader sysin
    5. = new BufferedReader(new InputStreamReader(System.in));
    6. String str = null;
    7. try {
    8. while ((str = sysin.readLine()) != null) {
    9. System.out.println(str);
    10. }
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }

    □ 実行結果

    $ java Sysin2Sysout
    入力データ <-- 入力したデータ  
    入力データ <-- 出力されたデータ
    出力データ <-- 入力したデータ  
    出力データ <-- 出力されたデータ
    
Top
■実習
  1. 上記アプリケーション"Sysin2Sysout.java"をつぎのように改造してください。("Sysin2Sysout02.java")
  2. □ 実行結果

    $ java Sysin2Sysout02
    Hello
    out: Hello
    err: Hello
    Java
    out: Java
    err: Java
    World!
    out: World!
    err: World!
    
  3. 標準入力から数字(整数)を入力して、その合計を標準出力に出すアプリケーション"Sum11.java"を作成してください。
    入力の終了は、"q" または "Q" キーの押下によることとします。
    1. import java.io.*;
    2. public class Sum11 {
    3. public static void main (String[] args) {
    4. BufferedReader sysin = /* コードを記述してください。*/ ;
    5. String str = null;
    6. int sum = 0;
    7. try {
    8. while (/* コードを記述してください。*/) {
    9. if (/* コードを記述してください。*/) {
    10. break;
    11. }
    12. // コードを記述してください。
    13. }
    14. } catch (IOException e) {
    15. System.out.println("IOException");
    16. } catch (Exception e) {
    17. e.printStackTrace();
    18. }
    19. System.out.println("sum = " + sum);
    20. }
    21. }

    □ 実行結果

    $ java Sum11
    22
    44
    66
    -99
    q
    sum = 33
    
  4. (オプション)
    上記"Sum11.java"を改造して、入力に数字以外が入力されたら、標準エラー出力にメッセージを出力するアプリケーション"Sum12.java"を作成してください。
    入力の終了は、"q" または "Q" キーの押下によることとします。
    1. import java.io.*;
    2. public class Sum12 {
    3. public static void main (String[] args) {
    4. BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
    5. String str = null;
    6. int sum = 0;
    7. try {
    8. // コードを記述してください。
    9. } catch (IOException e) {
    10. System.err.println("IOException");
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. }
    14. System.out.println("sum = " + sum);
    15. }
    16. }

    □ 実行結果

    $ java Sum12
    22
    44
    rr
    NumberFormatException for input rr
    66
    ii
    NumberFormatException for input ii
    -99
    qq
    NumberFormatException for input qq
    q
    sum = 33
    
  5. Linuxのgrepコマンドのように、標準入力から入れたテキスト・データから、コマンドラインで指定した文字列が含まれている行を標準出力に出力するアプリケーション"Grep01.java"をを作成してください。
    1. import java.io.*;
    2. public class Grep01 {
    3. public static void main (String[] args) {
    4. BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
    5. String str = null;
    6. try {
    7. // コードを記述してください。
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. }

    □ 実行結果

    $ cat Grep01.java | java Grep01 str
                    String str = null;
                            while ((str = sysin.readLine()) != null) {
                                    if (str.matches(".*" + args[0] + ".*")) {
                                            System.out.println(str);
    
  6. (オプション)
    じゃんけんゲームをするアプリケーション"Janken01.java"を作成してください。ゲームは、標準入力からグー(g)、チョキ(c)、パー(p)のいずれかを入力します。ゲームをやめるときには、xを入力します。そのとき、何勝何敗何分かを画面出力してください。
    1. import java.io.*;
    2. public class Janken01 {
    3. static BufferedReader br = null;
    4. public static void main(String[] args) {
    5. int draw = 0;
    6. int win = 0;
    7. int lose = 0;
    8. try {
    9. int user = -1;
    10. br = new BufferedReader(new InputStreamReader(System.in));
    11. while ((user = read()) != -1) {
    12. int computer = (int)(Math.random() * 3);
    13. // コードを記述してください。
    14. System.err.println(win + " 勝 " + lose + " 敗 " + draw + " 分 ");
    15. }
    16. System.out.println();
    17. System.out.println(win + " 勝 " + lose + " 敗 " + draw + " 分 ");
    18. }
    19. catch (IOException e) {
    20. e.printStackTrace();
    21. } finally {
    22. try {
    23. br.close();
    24. } catch (Exception e) {
    25. }
    26. }
    27. }
    28. static int read() throws IOException {
    29. int user = -1;
    30. while ((user = read("グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> ")) != -1) {
    31. if (user >= 0 && user <= 3) {
    32. break;
    33. }
    34. }
    35. return user;
    36. }
    37. static int read(String message) throws IOException {
    38. System.out.print(message);
    39. String str = br.readLine();
    40. if (str == null) {
    41. return -1;
    42. }
    43. int user = -1;
    44. // コードを記述してください。
    45. }
    46. }

    □ 実行結果

    $ java Janken01
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> g
    あなたの勝
    1 勝 0 敗 0 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> g
    あなたの敗け
    1 勝 1 敗 0 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> c
    引き分け
    1 勝 1 敗 1 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> c
    引き分け
    1 勝 1 敗 2 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> p
    あなたの勝
    2 勝 1 敗 2 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> p
    あなたの勝
    3 勝 1 敗 2 分
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> d
    グー(g), チョキ(c), パー(p), 終了(x)を入力してください。==> x
    
    3 勝 1 敗 2 分
    
  7. (オプション)
    数あてゲームをするアプリケーション"NumberGuess01.java"を作成してください。
    ゲームは、コンピューターが選んだ3桁の数字に対して、標準入力から3桁の数字を入力します。なお、3桁の数字には同じ数字はないものとします。アプリケーションは、桁は間違っているが、一致している数字の数と、桁もあっている数字の数を画面出力します。3桁の数字があえばゲーム終了です。このとき、何回で当てたかを画面出力します。 ださい。
    1. import java.io.*;
    2. public class NumberGuess01 {
    3. private static BufferedReader br = null;
    4. public static void main(String[] args) {
    5. int computer = getNumber();
    6. System.err.println("computer = " + computer);
    7. int[] n = new int[2];
    8. try {
    9. br = new BufferedReader(new InputStreamReader(System.in));
    10. int user;
    11. int count = 0;
    12. while ((user = read()) != -1) {
    13. n = checkNumbers(user, computer);
    14. if (user == computer) {
    15. System.out.println("ビンゴ! " + count);
    16. break;
    17. }
    18. System.out.println("桁違いの同じ数 = " + n[0]);
    19. System.out.println("  一致した数 = " + n[1]);
    20. System.out.println();
    21. }
    22. }
    23. catch (IOException e) {
    24. e.printStackTrace();
    25. } finally {
    26. try {
    27. br.close();
    28. } catch (Exception e) {
    29. }
    30. }
    31. }
    32. static int[] checkNumbers(int u, int c) {
    33. // コードを記述してください。
    34. return n;
    35. }
    36. static int getNumber() {
    37. // コードを記述してください。
    38. }
    39. static int read() throws IOException {
    40. int user = -1;
    41. while ((user = read("2桁-3桁の正整数をを入力してください。==> ")) != -1) {
    42. if (user >= 10 && user <= 999) {
    43. break;
    44. }
    45. }
    46. return user;
    47. }
    48. static int read(String message) throws IOException {
    49. System.out.print(message);
    50. String str = br.readLine();
    51. if (str == null) {
    52. return 0;
    53. }
    54. int user = 0;
    55. try {
    56. user = Integer.parseInt(str);
    57. } catch (NumberFormatException e) {
    58. }
    59. return user;
    60. }
    61. }

    □ 実行結果

    $ java NumberGuess01 2> null
    2桁-3桁の正整数をを入力してください。==> 123
    桁違いの同じ数 = 1
      一致した数 = 0
    
    2桁-3桁の正整数をを入力してください。==> 124
    桁違いの同じ数 = 0
      一致した数 = 0
    
    2桁-3桁の正整数をを入力してください。==> 345
    桁違いの同じ数 = 0
      一致した数 = 1
    
    2桁-3桁の正整数をを入力してください。==> 367
    桁違いの同じ数 = 1
      一致した数 = 1
    
    2桁-3桁の正整数をを入力してください。==> 378
    桁違いの同じ数 = 0
      一致した数 = 2
    
    2桁-3桁の正整数をを入力してください。==> 379
    ビンゴ! 6
    
Top
■商品管理システム
  1. つぎの"TestProduct.java"を作成し、実行してください。
    1. // 1602
    2. // package application;
    3. // import product.*;
    4. import java.util.*;
    5. import java.io.*;
    6. // 商品クラスをテストするクラスです。
    7. public class TestProduct {
    8. public static void main(String[] args) {
    9. TreeMap p = new TreeMap();
    10. SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
    11. System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
    12. System.out.println();
    13. AdminProduct ap = new AdminProduct();
    14. ap.read(s, args[0]);
    15. System.out.println("商品のデータを入力しました。 p = " + p);
    16. System.out.println("セット商品のデータを設定しました。 s = " + s);
    17. BufferedReader br = null;
    18. try {
    19. br = new BufferedReader(new InputStreamReader(System.in));
    20. String str = null;
    21. while ((str = br.readLine()) != null && !str.toUpperCase().equals("X")) {
    22. System.out.println("商品コード " + str + " の商品は " + s.query(str) + " です。");
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. } finally {
    27. try {
    28. br.close();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }
    33. ap.write(s, args[1]);
    34. System.out.println("セット商品のデータをファイルに書き出しました。 s = " + s);
    35. }
    36. }
    A	A001	パソコン	188000
    A	P001	プリンター	24000
    A	C001	CD-ROM	6500
    A	M001	USBメモリー	9800
    A	H001	ハードディスク	9200
    U	C001	DVD-ROM	9200
    D	M001
    

    □ 実行結果

    $ java -cp classes application.TestProduct products.txt out.txt
    セット商品のインスタンスを生成しました。 s = [SA01, パソコン・セット, 10, 0]
    
    商品のデータを入力しました。 p = {A001=[A001, パソコン, 188000], C001=[C001, DVD
    -ROM, 9200], H001=[H001, ハードディスク, 9200], P001=[P001, プリンター, 24000]}
    セット商品のデータを設定しました。 s = [SA01, パソコン・セット, 10, 207360]
    A001
    商品コード A001 の商品は [A001, パソコン, 188000] です。
    B001
    商品コード B001 の商品は [Not Found] です。
    C001
    商品コード C001 の商品は [C001, DVD-ROM, 9200] です。
    
    A001	パソコン	188000
    C001	DVD-ROM	9200
    H001	ハードディスク	9200
    P001	プリンター	24000
    
Top

inserted by FC2 system