入力ストリーム | 出力ストリーム | |
---|---|---|
クラス | InputStream FileInputStream |
OutputStream FileOutputStream |
コンストラクター | FileInputStream(String name) | FileOutputStream(String name) FileOutputStream(String name, boolean append) |
入出力メソッド | int read() | void write(int b) |
使用例 | while ((b = fis.read()) != -1) { ... } |
fos.write(b); |
クローズ | void close() | void close() |
- import java.io.*;
- public class Stream011 {
- public static void main(String[] args) {
- FileOutputStream fos = null;
- try {
- // "Stream.dat" ファイルのオープン
- fos = new FileOutputStream("Stream.dat");
- for (int b = 0; b < Integer.parseInt(args[0]); b++) {
- // b の最下位バイトの出力
- fos.write(b);
- }
- // ファイルのクローズ
- if (fos != null) fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Windows: \"debug Stream.dat の d オプション\" で確認してください。");
- System.out.println(" Linux: \"od -A x -t x1 Stream.dat\" で確認してください。");
- }
- }
$ 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 (終了)
- import java.io.*;
- public class Stream012 {
- public static void main(String[] args) {
- // ここにコードを記述してください。
- }
- }
$ 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 (終了)
- import java.io.*;
- public class Stream02 {
- public static void main(String[] args) {
- int b;
- FileInputStream fis = null;
- try {
- // "Stream.dat" ファイルのオープン
- fis = new FileInputStream("Stream.dat");
- while ((b = fis.read()) != -1) {
- System.out.print(Integer.toHexString(b) + " ");
- }
- // ファイルのクローズ
- if (fis != null) fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println();
- }
- }
$ java Stream02 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13
$ 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
- import java.io.*;
- public class FileIO011 {
- public static void main( String[] args ) {
- int b;
- FileInputStream fis = null;
- FileOutputStream fos = null;
- // ここにコードを記述してください。
- }
- }
$ java FileIO11 Stream.dat Stream.copy A A-- 出力ファイル名 +-- 入力ファイル名 $ diff Stream.dat Stream.copy <-- 差があるか否かの確認(なければメッセージなし)
$ java FileIO11 Stream.dat Stream.copy A A-- 出力ファイル名 +-- 入力ファイル名 $ dir Stream.* <-- 差があるか否かの確認(サイズで確認)
- import java.io.*;
- public class Encoding04 {
- public static void main(String[] args) {
- FileInputStream fis;
- FileOutputStream fos;
- int key = Integer.parseInt(args[2]);
- int b;
- try {
- fis = new FileInputStream(args[0]);
- fos = new FileOutputStream(args[1]);
- while ((b = fis.read()) != -1) {
- // ここにコードを記述してください。
- }
- fos.close();
- fis.close();
- } catch (IOException e) {
- }
- }
- }
□ 実行結果
$ java Encoding04 sample.txt sample.out 3
FileInputStream fis = new FileInputStream(ファイル名); DataInputStream dis = new DataInputStream(fis); FileOutputStream fos = new FileOutputStream(ファイル名); DataOutputStream dos = new DataOutputStream(fos);
(*) ストリームの終わりは、EOFException をスローすることで検出します。
(**) ストリームの終わりは、-1 を戻します。
- import java.io.*;
- public class FilterStream01 {
- public static void main(String[] args) {
- int n = Integer.parseInt(args[0]) * 1024;
- int b = 0;
- FileInputStream fis = null;
- FileOutputStream fos = null;
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- try {
- File tmpFile = File.createTempFile("tmp", ".tmp");
- fos = new FileOutputStream(tmpFile);
- bos = new BufferedOutputStream(fos);
- fis = new FileInputStream(tmpFile);
- bis = new BufferedInputStream(fis);
- long t1 = System.currentTimeMillis();
- for (b = 0; b < n; b++) {
- fos.write(b);
- }
- while ((b = fis.read()) != -1) {
- }
- long t2 = System.currentTimeMillis();
- for (b = 0; b < n; b++) {
- bos.write(b);
- }
- while ((b = bis.read()) != -1) {
- }
- long t3 = System.currentTimeMillis();
- fis.close();
- fos.close();
- System.out.println(" Node Stream = " + (t2 - t1) + "ms");
- System.out.println("Filter Stream = " + (t3 - t2) + "ms");
- System.out.println("Filter / Node = " + ((double)(t3 - t2) * 100 / (double)(t2 - t1)) + "%");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
□ 実行結果
$ java FilterStream01 100 Node Stream = 591ms Filter Stream = 20ms Filter / Node = 3.3840947546531304%