私は多元的な配列に未加工のリソーステキストファイルを読む方法を教えようとしています。生のテキストファイルは次のとおりです。FileReaderでraw txtファイルを参照するには?
ken todd
stuart brian
bob stewart
orange apple
green red
コードは以下のとおりです。
public void LoadFromFile(){
// int fileId = (getResources().getIdentifier("text_file",
// "raw", getPackageName()));
Scanner scan = new Scanner(new FileReader(R.raw.text_file);
// initialises the scanner to read the text_file
String[][] entries = new String[6][2];
// creates a 2d array with 6 rows and 2 columns.
int i = 0;
while (scan.hasNextLine()) {
entries[i] = scan.nextLine().split("\\s+");
i++;
}
//loops through the file and splits on a space
for (int row = 0; row < entries.length; row++) {
for (int col = 0; col < entries[0].length; col++) {
if (entries[row][col] != null) {
System.out.print(entries[row][col] + " ");
}
}
if (entries[row][0] != null) {
System.out.print("\n");
}
}
//prints the contents of the array that are not "null"
}
スキャナのラインにエラーが発生しました。具体的にFileReaderの(R.raw.text_file)私は明らかにFileReaderのがintを探していると私は、ファイルIDを入れていることを実行しようとしました
'FileReaderの(int型)'
がコンストラクタを解決できません取得しますintをint型変数に変換してから、FileReaderでint型変数を使用しても、どちらも機能しませんでした。
int fileId = (getResources().getIdentifier("text_file", "raw", getPackageName())); Scanner scan = new Scanner(new FileReader(fileId);
それは私に
を同じエラーを与えたコンストラクタ 'FileReaderの(int型)' を解決できません
私はここで間違って何をしているのですか?誰もがファイルを配列に入れる方法のヒントを得ましたか?
乾杯
エアフィックス
ダグ、私はあなたの助けを用いて溶液を得るために近づいています。ここの私の要点を見てください。 https://gist.github.com/Airfixed/799e784696b0a60c5423d347bf33a341しかし、txtファイルに空白行がある場合は動作しません。空白行はどうやって説明できますか? – Airfix