在Java中,InputStreamReader类是用于将字节流转换为字符流的。以下是一些InputStreamReader的常见用法:
InputStreamReader reader = new InputStreamReader(System.in);
int character = reader.read();
System.out.println("Read character: " + (char) character);
FileInputStream fileInputStream = new FileInputStream("file.txt");
InputStreamReader reader = new InputStreamReader(fileInputStream);
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
FileInputStream fileInputStream = new FileInputStream("file.txt");
InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8");
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
FileInputStream fileInputStream = new FileInputStream("file.txt");
InputStreamReader reader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
这些只是InputStreamReader的一些常见用法,根据具体的需求和场景,还可以进行更多的操作和定制。