Java中读取文件流的方法有多种,可以根据不同的需求选择合适的方法,常见的几种方法如下:
FileInputStream fis = new FileInputStream("file.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
int c;
while ((c = isr.read()) != -1) {
System.out.print((char) c);
}
isr.close();
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
File file = new File("file.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
FileInputStream fis = new FileInputStream("file.bin");
int b;
while ((b = fis.read()) != -1) {
System.out.print(b);
}
fis.close();
以上是几种常见的读取文件流的方法,根据具体的需求可以选择适合的方法。