转化流Demon02

发布时间 2023-04-26 17:52:40作者: 磊子记
package test2;

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();
}
}
}




}
}