44. バイトストリーム
44. バイトストリーム
バイナリの書き込み
L:\lesson112\test.java
import java.io.*; class test { public static void main(String args[]) { byte ary[] = { 0x41 , 0x42 , 0x43 , 0x44 }; try { FileOutputStream fos = new FileOutputStream("test1.txt"); fos.write(ary); fos.close(); DataOutputStream dos = new DataOutputStream(new FileOutputStream("test2.txt")); dos.writeBoolean(true); dos.writeBoolean(false); dos.writeInt(1023); dos.close(); } catch(IOException e) { } } }
実行結果
L:\lesson112>java testL:\lesson112>type test1.txt
ABCD
L:\lesson112>debug test2.txtMicrosoft (R) KKCFUNC バージョン 1.10
Copyright (C) Microsoft Corp. 1991,1993. All rights reserved.KKCFUNC が組み込まれました.
マイクロソフトかな漢字変換 バージョン 2.51
(C)Copyright Microsoft Corp. 1992-1993
- d
2DD8:0100 01 00 00 00 03 FF 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0110 00 00 00 00 00 00 00 00-00 00 00 00 34 00 C7 2D ............4..-
2DD8:0120 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0130 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0140 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0150 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0160 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2DD8:0170 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
- q
バイナリの読み込み
L:\lesson113\test.java
import java.io.*; class test { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream(args[0]); for (int i = 1; i <= 4; i++) System.out.println(fis.read()); // 1バイトずつ読み込み fis.close(); DataInputStream dis = new DataInputStream(new FileInputStream(args[0])); System.out.println(dis.readInt()); // int型として読み込み dis.close(); } catch(IOException e) {} } }
実行結果
L:\lesson113>java test ..\lesson112\test1.txt
65
66
67
68
1094861636