本帖最后由 Chaiber 于 2013-1-2 11:19 编辑
import java.io.*;
class CopyText
{
public static void main(String[] args)
{
copy_2();
}
public static void copy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("ThreadDemo_copy.txt");
fr = new FileReader("ThreadDemo.java");
char[] buf = new char[1024];
int len = 0;
while((len=fr.read(buf))!=-1);
{
fw.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("读写失败");
}
finally
{
if (fr!=null)
{
try
{
fr.close();
}
catch (IOException e)
{
}
}
if (fw!=null)
{
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
}
}
命令行的异常
D:\java\day18>java CopyText
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 CopyText.copy_2(CopyText.java:25)
at CopyText.main(CopyText.java:7)
文件拷贝出来了,可是里面没有东西,还有这个异常怎么解决,求解答,谢谢。
|
|