出现这种情况是为什么(请高手指点)

blueshk 2003-08-18 04:18:31

import java.io.*;
class file{
public static void main(String args[])throws Exception {
System.out.println("enter directory");
char ch;
StringBuffer dirbuf= new StringBuffer();
while ((ch=(char)System.in.read())!='\n')
dirbuf.append(ch);
File dir=new File (dirbuf.toString());
System.out.println("enter file name");
StringBuffer filebuf= new StringBuffer ();
while ((ch=(char)System .in.read())!='\n')
filebuf.append(ch);
File input=new File(dir,filebuf.toString());

if (input.exists())
{
System.out.println("\nfile found");
System.out.println("\nfile name:"+input.getName());
System.out.println("\n file path:"+input.getPath());
System.out.println("\n abs path:"+input.getAbsolutePath());
System.out.println("\n readable?:"+input.canRead());
System.out.println("\n writeable?:"+input.canWrite());
System.out.println("\n directory listing:");
String listing[]=dir.list();
for (int i=0; i<listing.length; i++)
System.out.println(listing[i]);
}
else { System.out.println("file not found");}
}
}
我编译通过了
可是我却得不到结果
我输入的 信息是 目录c:\exemple
文件名 input.txt
在这个目录下我建了input.txt这个文件 为什么结果确实 file no found
请高手指点。。。。。。。。。。。。
...全文
15 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueshk 2003-08-19
  • 打赏
  • 举报
回复
不是这的错把
你看我的结果:
D:\exemple>java file
enter directory
d:\exemple
enter file name
liuyc.java
file not found
我的文件下的确有这个文件
我用引数传递参数 import java.io.*;
class fileInfo{

public static void main(String args[]) throws IOException{
File fileToCheck;
if (args.length>0)
{ for (int i=0;i<args.length;i++)
{ fileToCheck = new File(args[i]);
info (fileToCheck); } }
else{ System.out.println("No file given."); } }
public static void info (File f) throws IOException {
System.out.println("Name: "+f.getName());
System.out.println("Path: "+f.getPath());
if (f.exists()) { System.out.println("File exists.");
System.out.print((f.canRead() ?" and is Readable":""));
System.out.print((f.canWrite()?" and is Writeable":""));
System.out.println(".");
System.out.println("File is " + f.length() + " bytes."); }
else { System.out.println("File does not exist."); } } }

运行结果是:
D:\exemple>java fileInfo d:\exemple\liuyc.java
Name: liuyc.java
Path: d:\exemple\liuyc.java
File exists.
and is Readable and is Writeable.
File is 244 bytes.
我真的高不懂上面的那个程序到底是哪错了
geyf 2003-08-18
  • 打赏
  • 举报
回复
你到dos方式下dir看看你的c:\exemple目录下的文件
怀疑是不是input.txt.txt啊?

刚才运行你的代码了,我这正常,输出“file found”
blueshk 2003-08-18
  • 打赏
  • 举报
回复
不可能呀
我试了很多边了 还是不好使
geyf 2003-08-18
  • 打赏
  • 举报
回复
这个做法是没有问题的,看看是不是名字错了,或者多了什么符号?
c:\exemple 是不是应该为 c:\example
liqun925 2003-08-18
  • 打赏
  • 举报
回复
你看看Jbuilder 6 的DEMO的TextEdit的代码吧!
或许有结果
stonewang 2003-08-18
  • 打赏
  • 举报
回复
up

62,612

社区成员

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

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