:: reverse dictionary ::
※ソースファイルについて
◎各マークの意味
Pb:pubic Pr:protected S:static
ファイルに1文字ずつ書き込むには、BufferedReader クラスの write メソッドを使います。
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter;
FileOutputStream stream = null; OutputStreamWriter osr = null; BufferedWriter bw = null; // 書き込む文字 char[] chars = new char[] {'A', 'B', 'C'}; File file = new File("sample.txt"); try { stream = new FileOutputStream(file); osr = new OutputStreamWriter(stream); bw = new BufferedWriter(osr); for (int i = 0; i < chars.length; i++) { // 書き込み bw.write((int)chars[i]); bw.newLine(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } }
実行すると sample.txt の内容が
A B C
になります。
全角文字も1文字ずつ書き込むことができます。
FileOutputStream stream = null; OutputStreamWriter osr = null; BufferedWriter bw = null; // 書き込む文字 char[] chars = new char[] {'A', 'B', 'C'}; File file = new File("sample.txt"); try { stream = new FileOutputStream(file); osr = new OutputStreamWriter(stream); bw = new BufferedWriter(osr); for (int i = 0; i < chars.length; i++) { // 書き込み bw.write((int)chars[i]); bw.newLine(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } }
実行すると sample.txt の内容が
A B C
になります。
java.io.BufferedWriter Pb void write(int c) throws IOException
文字 c を BufferedWriter オブジェクトの出力ストリームに書き込みます。
c … 書き込む文字
IOException
Copyright (C) 2005-2007 Noto Watabe. All rights reserved.
e-mail:wmh@always-pg.com