转换流
InputStreamReader
OutputStreamWriter
属于 1.处理流 2.字符流
作用:用来转换 字节流到字符流,或者字符流到字节流
从字节流读成字符流 输出到控制台
package com.daijunyi.file;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*转换流
* InputStreamReader
* OutputStreamWriter
*
* 属于 1.处理流 2.字符流
* 作用:用来转换 字节流到字符流,或者字符流到字节流
* */
public class InputStreamReaderTest {
public static void main(String[] args) {
InputStreamReader isr = null;//属于直接用默认的编码集
try {
FileInputStream fis = new FileInputStream("hello.txt");
isr = new InputStreamReader(fis);
char[] buffer = new char[10];
int len;
while ((len = isr.read(buffer)) != -1){
System.out.print(new String(buffer,0,len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
utf-8格式的文本文件,转换成gbk编码的文本文件
package com.daijunyi.file;
import java.io.*;
public class OutputStreamWriterTest {
public static void main(String[] args) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
FileInputStream fis = new FileInputStream("hello.txt");
FileOutputStream fos = new FileOutputStream("hello2.txt");
isr = new InputStreamReader(fis, "utf-8");
osw = new OutputStreamWriter(fos, "gbk");
char[] buffer = new char[10];
int len;
while ((len = isr.read(buffer)) != -1){
osw.write(buffer,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();
}
}
}
}