import java.io.*;
public class 拷贝
{
public static void main(String[] args)
{
String[] str = {"123456"};
File fid = new File("D\\a.txt"); //创建地址,
File did = new File("D\\b.txt");
FileWriter fod = null; //创建文件写入对象
FileReader dod = null; //创建文件读取对象
if ( fid.exists() )
{
System.out.println("文件已经存在");
}
else
{
try
{
fid.createNewFile();
System.out.println("文件创建成功");
} catch (Exception e)
{
}
}
try
{
fod = new FileWriter("D\\\\a.txt");
for (int i=0; i<str.length; i++)
{
fod.write(str[i]);
}
} catch (Exception e)
{
}
finally
{
try
{
fod.close();
} catch (IOException e2)
{
}
}
try
{
dod = new FileReader(fid);
String ctr = null;
while ( (ctr = dod.read() ) != null )
{
System.out.println(ctr);
}
} catch (Exception e)
{
// TODO: handle exception
} finally
{
try
{
dod.close();
} catch (Exception e2)
{
// TODO: handle exception
}
}
}
}
这么多try catch ,还不如直接抛一个大个的异常
异常没处理
dod.read()这个的返回值是int,不能这样读取字符串
https://blog.csdn.net/sutaotao_s77/article/details/79251364
修改为ctr = dod.readLine()