package com.atguigu.java;
import org.junit.Test;
import java.io.*;
public class InputStreamReaderTest {
@Test
public void test2(){
//造文件、造流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("srcTest.txt");
fos = new FileOutputStream("destTest-gbk.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//读写的过程
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1){
// String s =new String(cbuf,0,len);
// System.out.print(s); //这样测试了一下,控制台上是可以打印出来的
osw.write(cbuf,0,len); //就这,莫名其妙不能写入到,好气
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
jdk-8.0 代码如上,想将srcTest.txt文件里的内容输出到destTest-gbk.txt里,并将原来的utf-8编码格式转换为gbk。 在读取文件后测试了一下能将原文件的内容打印到控制台,但是生成的destTest-gbk文件里空空如也...连乱码也没有... 不晓得我是哪一步出错了。