如何将字节流转成字符流?
private static void byteToCharacter() {
try {
// 字节流
FileInputStream fis = new FileInputStream("book.xml");
//将字节流转成(字符流)
InputStreamReader isr = new InputStreamReader(fis);
// 缓冲流
BufferedReader br = new BufferedReader(isr);
//存放数据
String str = null;
//遍历数据
if ((str = br.readLine()) != null) {
System.out.println(str);
}
//关闭流
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
book.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="12">
<name>thinking in java</name>
<price>85.5</price>
</book>
<book id="15">
<name>Spring in Action</name>
<price>39.0</price>
</book>
</books>