:: reverse dictionary ::
※ソースファイルについて
◎各マークの意味
Pb:pubic Pr:protected S:static
ファイルから文字列で1行ずつ読み込むには、BufferedReader クラスの readLine メソッドを使います。
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader;
FileInputStream stream = null; InputStreamReader isr = null; BufferedReader br = null; String[] strs = new String[10]; // 読み込む文字列 File file = new File("sample.txt"); try { stream = new FileInputStream(file); isr = new InputStreamReader(stream); br = new BufferedReader(isr); int count = 0; String read; while ((read = br.readLine()) != null) { // 読み込み strs[count] = read; count++; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } }
sample.txt の内容が
1行目 2行目 3行目
となっている場合、strs[0] は "1行目" 、strs[1] は "2行目" 、strs[2] は "3行目" になります。
java.io.BufferedReader Pb String readLine() throws IOException
BufferedReader オブジェクトの入力ストリームから文字列で1行を読み込みます。
・読み込む行がある … 読み込んだ文字列
・読み込む行がない(入力ストリームの終わり) … null
IOException
Copyright (C) 2005-2007 Noto Watabe. All rights reserved.
e-mail:wmh@always-pg.com