求助一行代码

sanmaoqing 2014-03-04 01:46:22
File file=new File(".");
System.out.println(file.getName());//为啥获取文件名的时候输出的事一个点?上行代码的点不是以当前路径来创建一个File对象吗?实在没搞懂 ,求大神指点
System.out.println(file.getParent());
...全文
299 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanmaoqing 2014-03-08
  • 打赏
  • 举报
回复
引用 10 楼 wp19901108 的回复:
File f=new File("."); 默认的就是当前Java文件路径啊,文件名是点。 如果要在其他地方创建: //文件名 String name = "face.gif"; //源文件的路径 File f = new File("d:\\"+name); 如果路径"d:\\"中含.,那.表示当前路径,不过要注意写法。
哦 ,我懂了,非常感谢!
  • 打赏
  • 举报
回复
File f = new File(“D:\\1.txt”); f.getName 你这样输出 就是 1.txt!
wp19901108 2014-03-07
  • 打赏
  • 举报
回复
File f=new File("."); 默认的就是当前Java文件路径啊,文件名是点。 如果要在其他地方创建: //文件名 String name = "face.gif"; //源文件的路径 File f = new File("d:\\"+name); 如果路径"d:\\"中含.,那.表示当前路径,不过要注意写法。
Utend 2014-03-06
  • 打赏
  • 举报
回复
file只是内存里的一个对象,不是实际储存在硬盘的文件
吹胡子爷爷 2014-03-05
  • 打赏
  • 举报
回复
看File 的构造方法 File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. 这样写似乎没有问题,我怀疑参数名和文件名是一样的。。。文件这块还没有了解。。。坐等大神
中华雪碧 2014-03-05
  • 打赏
  • 举报
回复
File file=new File("."); 创建了一个文件名为“.”的文件
sanmaoqing 2014-03-05
  • 打赏
  • 举报
回复
引用 4 楼 ljl434841 的回复:
[quote=引用 楼主 sanmaoqing 的回复:] File file=new File("."); System.out.println(file.getName());//为啥获取文件名的时候输出的事一个点?上行代码的点不是以当前路径来创建一个File对象吗?实在没搞懂 ,求大神指点 System.out.println(file.getParent());
你告诉它的文件名叫".",你说它返回给你的是什么?[/quote]那当前路径是这个java 文件所在的路径是吗?为什么呢?
Mr-稻帅 2014-03-04
  • 打赏
  • 举报
回复
引用 4 楼 ljl434841 的回复:
[quote=引用 楼主 sanmaoqing 的回复:] File file=new File("."); System.out.println(file.getName());//为啥获取文件名的时候输出的事一个点?上行代码的点不是以当前路径来创建一个File对象吗?实在没搞懂 ,求大神指点 System.out.println(file.getParent());
你告诉它的文件名叫".",你说它返回给你的是什么?[/quote]确实是这样的。
绝对在乎妮 2014-03-04
  • 打赏
  • 举报
回复
引用 楼主 sanmaoqing 的回复:
File file=new File("."); System.out.println(file.getName());//为啥获取文件名的时候输出的事一个点?上行代码的点不是以当前路径来创建一个File对象吗?实在没搞懂 ,求大神指点 System.out.println(file.getParent());
你告诉它的文件名叫".",你说它返回给你的是什么?
-江沐风- 2014-03-04
  • 打赏
  • 举报
回复
如果是用的eclipse,可以看一下File的源代码,ctrl+File一下!
tony4geek 2014-03-04
  • 打赏
  • 举报
回复
File file=new File("21212"); System.out.println(file.getName());//21212
tony4geek 2014-03-04
  • 打赏
  • 举报
回复
/**
     * Returns the name of the file or directory denoted by this abstract
     * pathname.  This is just the last name in the pathname's name
     * sequence.  If the pathname's name sequence is empty, then the empty
     * string is returned.
     *
     * @return  The name of the file or directory denoted by this abstract
     *          pathname, or the empty string if this pathname's name sequence
     *          is empty
     */
    public String getName() {
	int index = path.lastIndexOf(separatorChar);
	if (index < prefixLength) return path.substring(prefixLength);
	return path.substring(index + 1);
    }

62,621

社区成员

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

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