import java.io.*;
class TransStreamDemo
{
public static void main(String[] args)throws IOException
{
FileReader fr=new FileReader("haha.txt");
OutputStreamWriter osw=
new OutputStreamWriter(new FileOutputStream("haha.txt"),"utf-8");
//为什么同名就不行呢?同名会发生下面的异常?求各位大侠帮忙解释一下。
/*
Exception in thread "main" java.lang.IndexOutOfBoundsException
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:121)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at TransStreamDemo.main(TransStreamDemo.java:11)
*/
char[] buf=new char[10];
int len=fr.read(buf);
osw.write(buf,0,len);
osw.close();
fr.close();
}
}
|