【 解 説 】
バイナリでファイルを出力するには、以下のようになります。
【 例 文 】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/** * ファイル出力【バイナリ編】 * 引数として渡されたバイト配列をバイナリファイルに出力 * * @param String filename 出力ファイル名 * @param byte[] b 出力するバイト配列 * @return なし */ private void write(String filename, byte[] b) { BufferedOutputStream stream = null; try { File file = new File(DictionariesPlugin.getDefault().getStateLocation().toFile(), filename); FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (IOException e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } // ----------------------------------------------------------------------------------- //【使用例】 InputStream in = .....; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int j; while ((j = in.read(b)) != -1) { baos.write(b, 0, j); } byte[] pix = baos.toByteArray(); write("hoge.dat", pix); |
■お薦め参考関連資料
Sun、Sun Microsystems、サンのロゴマーク、Java、Solarisに関連するすべての商標およびロゴマークは、米国 Sun Microsystems, Inc. の米国およびその他の国における商標または登録商標です。