java 复制文件 为什么是空的

import java.io.*;

class Copy
{
public static void main(String[] args) throws IOException
{
FileWriter fw =null;
FileReader fr = null;

try
{
fw = new FileWriter("Copy_1.java");
fr = new FileReader("Copy.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("safasdasd");
}
finally
{
if(fr != null)
try
{
fr.close();
}
catch (IOException e)
{
if (fw != null)
{
}
try
{
fw.close();
}
catch (IOException e1)
{
}
}

}

}
}

第1个回答  2014-10-04

import java.io.*;

class Copy {
public static void main(String[] args) {
FileWriter fw = null;
FileReader fr = null;

try {
fw =  new FileWriter("Copy_1.java");
fr =   new FileReader("Copy.java");

char[] buf = new char[1024];
int len = 0;

while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) { //关闭流你关闭的时候出错注意看看

try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if (fr != null) //关闭流
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

追问

是fw和fr的关闭顺序原因吗?

追答

不是,和顺序没关系。你把关闭fw放在了fr关闭里面所有错了。二者是并行的。

第2个回答  2014-10-04
fw..flush()

有字节流记得刷新一下缓冲本回答被网友采纳
第3个回答  2014-10-04
我觉得运行到哪一步什么的应该用 sysout去测试一下.
第4个回答  2014-10-04
两个文件你是否弄反了!追问

没有 换过来提示错误!应该没反!

相似回答