Java中getAbsolutePath()什么意思?

 我来答
千锋教育
2016-01-13 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
getAbsolutePath():返回抽象路径名的绝对路径名字符串。

public static void test1(){
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());

}

-----默认相对路径:取得路径不同------
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
手机用户50996
2015-09-10 · 超过47用户采纳过TA的回答
知道答主
回答量:107
采纳率:0%
帮助的人:104万
展开全部
参见下面demo,就知道它的含义了。
getAbsolutePath():返回抽象路径名的绝对路径名字符串。
public static void test1(){
File file1 = new File(".\\test1.txt");
File file2 = new File("D:\\workspace\\test\\test1.txt");
System.out.println("-----默认相对路径:取得路径不同------");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println("-----默认绝对路径:取得路径相同------");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());

}

-----默认相对路径:取得路径不同------
.\test1.txt
D:\workspace\test\.\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt

public static void test2() throws Exception{
File file = new File("..\\src\\test1.txt");
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
}
D:\workspace\test\..\src\test1.txt
D:\workspace\src\test1.txt

public static void test3() throws Exception{
File file = new File("D:\\Text.txt");
System.out.println(file.getCanonicalPath());
}
(1),确定D盘下没有Text.txt这个文件,直接执行这段代码,得到的结果是:
D:\Text.txt注意这里试大写的Text.txt
(2)在D盘下建立一个文件,名叫text.txt,再次执行代码,得到结果
D:\text.txt同样的代码得到不同的结果。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式