在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的一些常見用法,根據具體的需求和場景,還可以進行更多的操作和定制。