转换流
将字节流转换为字符流:使用了装饰者设计模式
将字节输入流,转换为字符输入流:
public class Demo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("d://b.txt");
InputStreamReader isr =new InputStreamReader(fis);
char[] chars = new char[15];
int len = isr.read(chars);
//将内容读取到chars字符数组里
isr.read(chars);
System.out.println(new String(chars,0,len));
isr.close();
}
}
将字节流转换为字符流:使用了装饰者设计模式
将字节输入流,转换为字符输入流:
public class InputStreamReader {
public static void main(String[] args) throws IOException {
//这是创建了新文件流
FileOutputStream fis = new FileOutputStream("d://c.txt");
//创建字符流,向上面那个文件里写
OutputStreamWriter osw = new OutputStreamWriter(fis);
osw.write("天山鸟飞绝");
osw.flush();
osw.close();
}
}
输出结果:D盘新建了c.txt文件,并且里面有”天山鸟飞绝“内容、