【 解 説 】
テキストでファイルを出力するには、以下のようになります。
【 例 文 】
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 |
/** * ファイル出力【テキスト編】 * 引数として渡された文字列をテキストファイルに出力 * * @param String log 出力文字列 * @return boolean true -> OK, false -> NG */ private boolean write(String log) { BufferedWriter writer = null; try { // 第二引数がtrueなら追記、falseなら上書き。 writer = new BufferedWriter(new FileWriter("./log.txt", true)); writer.write(log, 0, log.length()); writer.newLine(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (writer != null) { writer.flush(); writer.close(); } } catch (Exception e) { e.printStackTrace(); } } return true; } |
■お薦め参考関連資料
Sun、Sun Microsystems、サンのロゴマーク、Java、Solarisに関連するすべての商標およびロゴマークは、米国 Sun Microsystems, Inc. の米国およびその他の国における商標または登録商標です。