转换流是处理流的一种
转化流提供了在字节流和字符流之间的转换
InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);
参数二指明了字符集,具体使用哪种字符集取决于文件保存时使用的字符集
举例:
InputStreamReader isr = new InputStreamReader(fis);//使用系统默认的字符集
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用UTF-8字符集
public OutputStreamWriter(OutputStream out, String charsetName)
举例
osw = new OutputStreamWriter(fos,"gbk");
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File file1 = new File("hello.txt");
File file2 = new File("hi.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"UTF-8");
osw = new OutputStreamWriter(fos,"gbk");
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
原文:https://www.cnblogs.com/CrabDumplings/p/13459733.html