マルチレコードファイルへのルックアップ検索を高速化するために、その要素を配列の文字列配列に格納して、「AF」のような文字列を同様の文字列のみ (「AA」、「AB、. .. 、"AZ") であり、ファイル全体ではありません。
元のファイルは次のようになります。
AA
ABC
AF
(...)
AP
BE
BEND
(...)
BZ
(...)
SHORT
VERYLONGRECORD
ZX
私が翻訳したい
AA ABC AF (...) AP
BE BEND (...) BZ
(...)
SHORT
VERYLONGRECORD
ZX
ソースファイルが時間内に変更される可能性があるため、レコードの数と各「行」に含まれる「要素」の数はわかりません(メモリに読み取られた後、配列が読み取られるだけであっても)。
私はWhisソリューションを試しました:
クラスで、次元を定義せずに (文字列) 配列の文字列配列を定義しました
public static String[][] tldTabData;
次に、別のクラスでファイルを読み取ります。
public static void tldLoadTable() {
String rec = null;
int previdx = 0;
int rowidx = 0;
// this will hold each row
ArrayList<String> mVector = new ArrayList<String>();
FileInputStream fStream;
BufferedReader bufRead = null;
try {
fStream = new FileInputStream(eVal.appPath+eVal.tldTabDataFilename);
// Use DataInputStream to read binary NOT text.
bufRead = new BufferedReader(new InputStreamReader(fStream));
} catch (Exception er1) {
/* if we fail the 1.st try maybe we're working into some "package" (e.g. debugging)
* so we'll try a second time with a modified path (e.g. adding "bin\") instead of
* raising an error and exiting.
*/
try {
fStream = new FileInputStream(eVal.appPath +
"bin"+ File.separatorChar + eVal.tldTabDataFilename);
// Use DataInputStream to read binary NOT text.
bufRead = new BufferedReader(new InputStreamReader(fStream));
} catch (FileNotFoundException er2) {
System.err.println("Error: " + er2.getMessage());
er2.printStackTrace();
System.exit(1);
}
}
try {
while((rec = bufRead.readLine()) != null) {
// strip comments and short (empty) rows
if(!rec.startsWith("#") && rec.length() > 1) {
// work with uppercase only (maybe unuseful)
//rec.toUpperCase();
// use the 1st char as a row index
rowidx = rec.charAt(0);
// if row changes (e.g. A->B and is not the 1.st line we read)
if(previdx != rowidx && previdx != 0)
{
// store the (completed) collection into the Array
eVal.tldTabData[previdx] = mVector.toArray(new String[mVector.size()]);
// clear the collection itself
mVector.clear();
// and restart to fill it from scratch
mVector.add(rec);
} else
{
// continue filling the collection
mVector.add(rec);
}
// and sync the indexes
previdx = rowidx;
}
}
streamIn.close();
// globally flag the table as loaded
eVal.tldTabLoaded = true;
} catch (Exception er2) {
System.err.println("Error: " + er2.getMessage());
er2.printStackTrace();
System.exit(1);
}
}
プログラムを実行すると、文字列が mVector に正しく蓄積されますが、それらを eVal.tldTabData にコピーしようとすると、NullPointerException が発生します。
ある時点で配列を作成/初期化する必要がありますが、どこでどのように把握するのに問題があります。
初めて Java でコーディングしています... helloworld 離れて。:-)