从书上拷贝下来的,怎么老是“can't find the file”,

netos 2004-07-20 11:58:20
import java.io.*;

class ReadFile
{
public static void main(String[] args)
{
System.out.println("Please input the directory name:");
char ch;
StringBuffer dirBuf = new StringBuffer();
try
{
while((ch= (char)System.in.read())!= '\n')
{
dirBuf.append(ch);
}
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}
File dir = new File(dirBuf.toString());

System.out.println("Please input the file name:");
StringBuffer fileBuf = new StringBuffer();
try
{
while((ch = (char)System.in.read())!='\n')
{
fileBuf.append(ch);
}
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}

File input = new File(dir,fileBuf.toString());

if (input.isFile()&&input.canRead()&&input.canWrite())
{
try
{
RandomAccessFile file = new RandomAccessFile(input,"rw");
System.out.println("File length:" + file.length());
System.out.println("Pointer pos:" + file.getFilePointer());
while (file.getFilePointer()< file.length())
{
System.out.println(file.readLine());
}
file.close();
}
catch(IOException e)
{
System.err.println("Exception : " + e.getMessage());
}
}
else
{
System.err.println("can't find the file");
}
}
}
...全文
132 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lulq21cn 2004-07-26
  • 打赏
  • 举报
回复
DataInputStream in =
new DataInputStream(
new BufferedInputStream(System.in));
String s;
try {
while((s = in.readLine()).length() != 0)
System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
netos 2004-07-23
  • 打赏
  • 举报
回复
Lulq21cn 2004-07-23
  • 打赏
  • 举报
回复
是挺奇怪的,我试过,在File input = new File(dir,fileBuf.toString());前
输出fileBuf的值为空,感觉象被丢掉了。
netos 2004-07-21
  • 打赏
  • 举报
回复
D:\temp\java>java ReadFile
Please input the directory name:
c:\Please input the file name:
auto.bat
can't find the file


/*
c:\auto.bat是存在的。如果我直接把"c:\\" 和 “auto.bat”作为参数放到
File input = new File(dir,fileBuf.toString());如
File input = new File("c:\\","auto.bat");
就一点问题没有。
*/
8ChenFeng8 2004-07-21
  • 打赏
  • 举报
回复
输入目录的时候末尾是不是没有加入"\"
zhuyinli 2004-07-21
  • 打赏
  • 举报
回复
请告知你的输入和实际的目标文件路径
netos 2004-07-21
  • 打赏
  • 举报
回复
liulangxiaozi 2004-07-21
  • 打赏
  • 举报
回复
应该是路径不正确。
8ChenFeng8 2004-07-21
  • 打赏
  • 举报
回复
我使用Sun One Studio+jdk 1.4编译通过,使用无误。但在命令行状态下确实是显示无法找到!
-----------刚刚学习,无法回答你,抱歉!

62,614

社区成员

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

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