ファイルがfあり、それを次のように変更する必要がありFileInputStream fsます。
File f = new File("C:/dir/foo.txt");
FileInputStream fs = (FileInputStream)f;
しかし、私はこのエラーが発生します:
Cannot cast from File to FileInputStream
fsのコンテンツを取得するにはどうすればよいfですか?
ファイルがfあり、それを次のように変更する必要がありFileInputStream fsます。
File f = new File("C:/dir/foo.txt");
FileInputStream fs = (FileInputStream)f;
しかし、私はこのエラーが発生します:
Cannot cast from File to FileInputStream
fsのコンテンツを取得するにはどうすればよいfですか?
トリックはこれです:
FileInputStream fs = new FileInputStream(f);
このアプローチを使用できます。
BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(new File("text.txt")))));
String line = null;
while ((line = reader.readLine()) != null) {
// do something with your read line
}
またはこれ:
byte[] bytes = Files.readAllBytes(Paths.get("text.txt"));
String text = new String(bytes, StandardCharsets.UTF_8);