わかりました、私のために物事を片付けてくれる人が必要です。
ファイルにアクセスしてファイルを読み取る 100 通りの方法を見てきました ( FileReader
)。
私はそれらすべてを試しましたが、正しく行う方法が見つかりません。
私が試してみると:
String path = Engine.class.getResource("Words.txt").toString();
また
URL url = getClass().getResource("Words.txt");
String path = url.getFile();
File myFile = new File(path);
私は直接行きます:
dispatchUncaughtException
誰もそれを行う良い方法に同意していないように見えるので、どこを見ればいいのかわかりません。また、そのような例外とは何ですか? とても簡単な作業なので、これを行う簡単な方法がある
はずです。プロジェクトのフォルダーにWords.txt
あるファイルをプログラムに表示させたいだけです。SRC
それが役立つ場合は完全なコード:
public String GetWord()
{
String [] Words = new String [10];
int random = (int)(Math.random() * 10);
URL url = getClass().getResource("Words.txt");
String path = url.getFile();
File myFile = new File(path);
try
{
FileReader myReader = new FileReader(myFile);
BufferedReader textReader = new BufferedReader(myReader);
for(int i = 0; i < 10; i++)
{
Words[i] = textReader.readLine();
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return Words[random];
}