帮助查看一下下面一段代码

rbaidu 2010-03-28 02:39:44
学习java的io,从书上摘抄一段代码调试运行,代码如下:

import java.io.*;

class CopyMaker
{
String sourceName,destName;
BufferedReader source;
BufferedWriter dest;
String line;

private boolean openFiles()
{
try
{
source=new BufferedReader(new FileReader(sourceName));
}
catch (IOException iox)
{
System.out.println("Problem opening "+sourceName);
return false;
}

try
{
dest=new BufferedWriter (new FileWriter (destName));
}
catch (IOException iox)
{
System.out.println("Problem opening "+destName);
return false;
}
return true;
}

private boolean copyFiles()
{
try
{
line=source.readLine();
while(line!=null)
{
dest.write(line);
dest.newLine();
line=source.readLine();
}
}
catch (IOException iox)
{
System.out.println("Problem reading or writing");
return false;
}
return true;
}

private boolean closeFiles()
{
boolean retVal=true;
try
{
source.close();
}
catch (IOException iox)
{
System.out.println("Problem closing "+sourceName);
retVal=false;
}

try
{
dest.close();
}
catch (IOException iox)
{
System.out.println("Problem closing "+destName);
retVal=false;
}
return retVal;
}

public boolean copy(String src,String dst)
{
sourceName=src;
destName=dst;
return openFiles()&©Files()&&closeFiles();
}
}

public class File1
{
public static void main(String[] args)
{
new CopyMaker().copy("f:/Hello.txt","f:/CopyHello.txt");
}
}
结果显示Problem opening f:/Hello.txt,请问为什么?
...全文
55 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rbaidu 2010-03-28
  • 打赏
  • 举报
回复
你说得对,我刚才摸索半天才想起来自己用的是F盘,刚学java,不知道连盘符也要区分大小写,只要有Hello.txt这个文件就行了,后面那个可以自动创建。非常感谢!
rypgood 2010-03-28
  • 打赏
  • 举报
回复
没错啊,我试了你的代码,运行没错,copy成功了
关键是你有没有f这么个盘,f盘下面有没有Hello.txt和CopyHello.txt两个文件
你可以自己试着,在f盘下面建立Hello.txt和CopyHello.txt两个文件,并且在Hello.txt里面写点东西
运行程序之后再看CopyHello.txt的内容

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧