转换流属于字符流
InputStreamReader 将一个字节的输入流转换为字符流
OutputStreamWritter 将一个字符的输出流转换为字节流
package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
public class zhuanhuanliu {
@Test
public void test1() throws IOException{
FileInputStream fis = new FileInputStream("hello.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
int len;
char[] ch = new char[20];
while((len = isr.read(ch))!=-1){
String string = new String(ch,0,len);
System.out.print(string);
}
}
@Test
public void test2() throws IOException{
File file1 = new File("");
File file2 = new File("");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
char[] ch = new char[2];
int len;
while((len = isr.read(ch)) != -1){
osw.write(ch,0,len);
}
isr.close();
osw.close();
}
}