package test2;标签:fos,isr,readData,转化,printStackTrace,Demon02,catch,null From: https://www.cnblogs.com/Leizi-go/p/17356858.html
import java.io.*;
//转换流
public class Demo03 {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
InputStreamReader isr = null;
try {
fos = new FileOutputStream("H:\\Java2234\\Test.txt");
fos.write("北京".getBytes("GBK"));
fos.write("北京".getBytes("UTF-8"));
fos.flush();
fis = new FileInputStream("H:\\Java2234\\Test.txt");
isr = new InputStreamReader(fis);
int readData;
while ((readData = isr.read()) != -1) {
System.out.println((char)readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}