Java - バイト・ストリーム


■ページ目次

Top

■バイト・ストリーム

Top
■InputStreamクラス
Top
■OutputStreamクラス
Top

■ノード・ストリームの使用法

Top
■入力ストリーム
■例題
  1. つぎの"Stream011.java"を作成ください。
    1. import java.io.*;
    2. public class Stream011 {
    3. public static void main(String[] args) {
    4. FileOutputStream fos = null;
    5. try {
    6. // "Stream.dat" ファイルのオープン
    7. fos = new FileOutputStream("Stream.dat");
    8. for (int b = 0; b < Integer.parseInt(args[0]); b++) {
    9. // b の最下位バイトの出力
    10. fos.write(b);
    11. }
    12. // ファイルのクローズ
    13. if (fos != null) fos.close();
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. System.out.println("Windows: \"debug Stream.dat の d オプション\" で確認してください。");
    18. System.out.println(" Linux: \"od -A x -t x1 Stream.dat\" で確認してください。");
    19. }
    20. }
    $ java Stream011 20
    Windows: "debug Stream.dat の d オプション" で確認してください。
      Linux: "od -A x -t x1 Stream.dat" で確認してください。
    
    $ od -A x -t x1 Stream.dat
    000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
    000010 10 11 12 13
    000014
    
    > java Stream011 20
    Windows: "debug Stream.dat の d オプション" で確認してください。
      Linux: "od -A x -t x1 Stream.dat" で確認してください。
    
    > debug Stream.dat
    Microsoft (R) KKCFUNC バージョン 1.10
    Copyright (C) Microsoft Corp. 1991,1993. All rights reserved.
    
    KKCFUNC が組み込まれました.
    
    マイクロソフトかな漢字変換  バージョン 2.51
    (C)Copyright Microsoft Corp. 1992-1993
    -d  (ダンプ出力)
    2440:0100  00 01 02 03 04 05 06 07-08 09 0A 0B 0C 0D 0E 0F   ................
    2440:0110  10 11 12 13 59 25 28 3B-5A 25 A1 3B 34 00 2F 24   ....Y%(;Z%.;4./$
                                      :
    -q  (終了)
    
■実習
  1. (オプション)
    上記
    "Stream011.java"をつぎのように改造して"Stream012.java"を作成ください。
    1. import java.io.*;
    2. public class Stream012 {
    3. public static void main(String[] args) {
    4. // ここにコードを記述してください。
    5. }
    6. }
    $ java Stream012 20
    $ od -A x -t x1 Stream.dat
    000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    000010 00 00 00 00
    000014
    
    > java Stream012 20
    > debug Stream.dat
    Microsoft (R) KKCFUNC バージョン 1.10
    Copyright (C) Microsoft Corp. 1991,1993. All rights reserved.
    
    KKCFUNC が組み込まれました.
    
    マイクロソフトかな漢字変換  バージョン 2.51
    (C)Copyright Microsoft Corp. 1992-1993
    -d  (ダンプ出力)
    2440:0100  00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00   ................
    2440:0110  00 00 00 00 59 25 28 3B-5A 25 A1 3B 34 00 2F 24   ....Y%(;Z%.;4./$
                                      :
    -q  (終了)
    
Top
■出力ストリーム
■例題
  1. つぎの"Stream02.java"をコンパイル・実行してください。
    1. import java.io.*;
    2. public class Stream02 {
    3. public static void main(String[] args) {
    4. int b;
    5. FileInputStream fis = null;
    6. try {
    7. // "Stream.dat" ファイルのオープン
    8. fis = new FileInputStream("Stream.dat");
    9. while ((b = fis.read()) != -1) {
    10. System.out.print(Integer.toHexString(b) + " ");
    11. }
    12. // ファイルのクローズ
    13. if (fis != null) fis.close();
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. System.out.println();
    18. }
    19. }
    $ java Stream02
    0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
    
■実習
  1. (オプション)
    "Stream02.java"をつぎのように改造して、"Stream021.java"を作成ください。

  2. $ java Stream021
    0 0 0
    1 1 1
    2 2 10
    3 3 11
    4 4 100
    5 5 101
    6 6 110
    7 7 111
    8 8 1000
    9 9 1001
    a 10 1010
    b 11 1011
    c 12 1100
    d 13 1101
    e 14 1110
    f 15 1111
    10 16 10000
    11 17 10001
    12 18 10010
    13 19 10011
    
Top
■実習
  1. つぎの"FileIO011.java"を作成ください。
    1. import java.io.*;
    2. public class FileIO011 {
    3. public static void main( String[] args ) {
    4. int b;
    5. FileInputStream fis = null;
    6. FileOutputStream fos = null;
    7. // ここにコードを記述してください。
    8. }
    9. }
    $ java FileIO11 Stream.dat Stream.copy
              A      A-- 出力ファイル名
              +-- 入力ファイル名
    $ diff Stream.dat Stream.copy  <-- 差があるか否かの確認(なければメッセージなし)
    
    $ java FileIO11 Stream.dat Stream.copy
              A      A-- 出力ファイル名
              +-- 入力ファイル名
    $ dir Stream.*  <-- 差があるか否かの確認(サイズで確認)
    
  2. コマンドラインから入力ファイル名、出力ファイル名、暗号化キーを指定し、入力ファイルを暗号化キーにしたがって、暗号化(または復号化)するアプリケーション"Encoding04.java"を作成してください。ここで、暗号化はcharの下位何ビットをビット反転するかは、暗号化キーによります。
    1. import java.io.*;
    2. public class Encoding04 {
    3. public static void main(String[] args) {
    4. FileInputStream fis;
    5. FileOutputStream fos;
    6. int key = Integer.parseInt(args[2]);
    7. int b;
    8. try {
    9. fis = new FileInputStream(args[0]);
    10. fos = new FileOutputStream(args[1]);
    11. while ((b = fis.read()) != -1) {
    12. // ここにコードを記述してください。
    13. }
    14. fos.close();
    15. fis.close();
    16. } catch (IOException e) {
    17. }
    18. }
    19. }

    □ 実行結果

    $ java Encoding04 sample.txt sample.out 3
    
Top

■基本データ型の入出力

Top
■DataInputStream
Top
■DataOutputStream
Top
■例題
Top

■参考

■FileInputStream と BufferedInputStream の速度比較
  1. FileInputStream と BufferedInputStream の速度を比較するアプリケーション"FilterStream01.java"です。コマンドラインから、入出力するキロバイト数を指定します。
    1. import java.io.*;
    2. public class FilterStream01 {
    3. public static void main(String[] args) {
    4. int n = Integer.parseInt(args[0]) * 1024;
    5. int b = 0;
    6. FileInputStream fis = null;
    7. FileOutputStream fos = null;
    8. BufferedInputStream bis = null;
    9. BufferedOutputStream bos = null;
    10. try {
    11. File tmpFile = File.createTempFile("tmp", ".tmp");
    12. fos = new FileOutputStream(tmpFile);
    13. bos = new BufferedOutputStream(fos);
    14. fis = new FileInputStream(tmpFile);
    15. bis = new BufferedInputStream(fis);
    16. long t1 = System.currentTimeMillis();
    17. for (b = 0; b < n; b++) {
    18. fos.write(b);
    19. }
    20. while ((b = fis.read()) != -1) {
    21. }
    22. long t2 = System.currentTimeMillis();
    23. for (b = 0; b < n; b++) {
    24. bos.write(b);
    25. }
    26. while ((b = bis.read()) != -1) {
    27. }
    28. long t3 = System.currentTimeMillis();
    29. fis.close();
    30. fos.close();
    31. System.out.println(" Node Stream = " + (t2 - t1) + "ms");
    32. System.out.println("Filter Stream = " + (t3 - t2) + "ms");
    33. System.out.println("Filter / Node = " + ((double)(t3 - t2) * 100 / (double)(t2 - t1)) + "%");
    34. } catch (Exception e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }

    □ 実行結果

    $ java FilterStream01 100
      Node Stream = 591ms
    Filter Stream = 20ms
    Filter / Node = 3.3840947546531304%
    
Top

inserted by FC2 system