テキストファイルを読み取って文字列に保存する必要があるJavaアプリケーションを実行していますが、NoSuchFileException
. テキスト ファイルは、src の横にある assets というフォルダーにあります。
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
これは、ここで見つけたファイルを読み取るメソッドです。
try {
string = readFile("test.txt",Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
「/assets/test.txt」やその他のバリエーションも試しましたが、役に立ちませんでした。