本帖最后由 淡淡柠檬茶 于 2014-6-6 16:21 编辑
//为什么copy_1提示(如下),我声明了啊
C:\Users\Administrator\Desktop\lianxi>javac CopyText.java
CopyText.java:8: 错误: 未报告的异常错误IOException; 必须对其进行捕获或声明以便抛
出
//将C盘的一个文本文件复制到D盘
//复制原来:将C盘下的文件数据存储到D盘下的一个文件中
import java.io.*;
class CopyText
{
public static void main(String[] args)
{
copy_1();
}
public static void copy_1() throws IOException
{
FileWriter fw = new FileWriter("123_copy.txt");
FileReader fr = new FileReader("123.txt");
int num = 0;
while ((num = fr.read())!=-1)
{
fw.write(num);
}
fw.close();
fr.close();
}
public static void copy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = new FileWriter("123_copy.txt");
fr = new FileReader("123.text");
char[] bvf = new char[3];
int len = 0;
while ((len = fr.read(bvf))!=-1)
{
fw.write(bvf,0,len);
}
}
catch (Exception e)
{
throw new RuntimeException("读写失败");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch (Exception e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch (Exception e)
{
}
}
}
}
|
|