首页 > 生活万象

getabsolutepath file.getabsolutepath

j*a 和 getAbsolutePath() 有中 file的getpath 啥区别

publicstaticvoidtest1(){

Filefile1=newFile(“.\\test1.txt”);

Filefile2=newFile(“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

因为getPath()得到的是构造file的时候的路径。

getAbsolutePath()得到的是全路径

如果构造的时候就是全路径那直接返回全路径

如果构造的时候试相对路径,返回当前目录的路径+构造file时候的路径

j*a getFileName() 为什么只剩下文件名

j*a io包中File类中并没有getFileName()方法,这里应该是指getName()方法,此方法仅返回文件名,并不会包含路径。如果需要完整路径+文件名,应该使用getPath()取得路径后再组合使用getName(),拼出一个完整路径+文件名的字符串。

==File类中常用方法如下,下文中file是File类的一个实例==

取得文件名称的方法: file.getName()

取得文件路径的方法: file.getPath()

判断文件是否绝对路径: file.isAbsolute()

取得文件的根目录: file.getParent()

判断文件是否存在: file.exists()

判断是否是目录: file.isDirectory()

判断是否是文件: file.isFile()

判断是否是隐藏文件: file.isHidden()

判断是否可读: file.canRead()

判断是否可写: file.canWrite()

android怎样拿到file的路径

Environment常用方法:

*方法:getDataDirectory()

解释:返回 File,获取 Android

数据目录。

*方法:getDownloadCacheDirectory()

解释:返回 File,获取 Android

下载/缓存内容目录。

*方法:getExternalStorageDirectory()

解释:返回 File,获取外部存储目录即

SDCard

*方法:getExternalStoragePublicDirectory(String type)

解释:返回 File

,取一个高端的公用的外部存储器目录来摆放某些类型的文件

*方法:getExternalStorageState()

解释:返回 File

,获取外部存储设备的当前状态

*方法:getRootDirectory()

解释:返回 File,获取 Android的根目录

file的getPath getAbsolutePath和getCanonicalPath的不同

File的这三个方法在api中都有说明,仅以程序为例说明。

package test;

import

j*a.io.File;

import j*a.io.IOException;

public class TestFilePath

{

public static void main(String[] args){

// TODO Auto-generated

methodstub

System.out.println(System.getProperty(“user.dir”));

try

{

System.out.println(“—–默认相对路径:取得路径不同——“);

File file1=new

File(“..\\src\\test1.txt”);

System.out.println(file1.getPath());

System.out.println(file1.getAbsolutePath());

System.out.println(file1.getCanonicalPath());

System.out.println(“—–默认相对路径:取得路径不同——“);

File

file=new

File(“.\\test1.txt”);

System.out.println(file.getPath());

System.out.println(file.getAbsolutePath());

System.out.println(file.getCanonicalPath());

System.out.println(“—–默认绝对路径:取得路径相同——“);

File

file2=new

File(“D:\\workspace\\test\\test1.txt”);

System.out.println(file2.getPath());

System.out.println(file2.getAbsolutePath());

System.out.println(file2.getCanonicalPath());

}

catch(IOException e){

// TODOAuto-generated catch

block

e.printStackTrace();

}

}

}

程序执行结果如下:

F:\eclipseworkspace\testejb

—–默认相对路径:取得路径不同——

..\src\test1.txt

F:\eclipseworkspace\testejb\..\src\test1.txt

F:\eclipseworkspace\src\test1.txt

—–默认相对路径:取得路径不同——

.\test1.txt

F:\eclipseworkspace\testejb\.\test1.txt

F:\eclipseworkspace\testejb\test1.txt

—–默认绝对路径:取得路径相同——

D:\workspace\test\test1.txt

D:\workspace\test\test1.txt

D:\workspace\test\test1.txt

结论:

当输入为绝对路径时,返回的都是绝对路径。

当输入为相对路径时:

getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减

getAbsolutePath()返回的其实是user.dir+getPath()的内容,从上面F:\eclipseworkspace\testejb、F:\eclipseworkspace\testejb\..\src\test1.txt、F:\eclipseworkspace\testejb\.\test1.txt可以得出。

getCanonicalPath()返回的就是标准的将符号完全解析的路径

public String

getAbsolutePath()返回抽象路径名的绝对路径名字符串。

如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath()

方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 user.dir

指定。否则,使用与系统有关的方式分析此路径名。在 UNIX系统上,通过根据当前用户目录分析某一相对路径名,可使该路径名成为绝对路径名。在 Microsoft

Windows

系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。

返回:

绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的

抛出:

SecurityException

-如果无法访问所需的系统属性值。

另请参见:

isAbsolute()

public String getCanonicalPath()

throws

IOException返回抽象路径名的规范路径名字符串。

规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调用

getAbsolutePath()方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比如”.”和

“..”)、分析符号连接(对于 UNIX平台),以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows

平台)。

表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。

返回:

表示与此抽象路径名相同的文件或目录的规范路径名字符串

抛出:

IOException

-如果发生 I/O错误(可能是因为构造规范路径名需要进行文件系统查询)

SecurityException-

如果无法访问所需的系统属性值,或者存在安全管理器,且其 SecurityManager.checkRead(j*a.io.FileDescriptor)

方法拒绝对该文件进行读取访问

从以下版本开始:

JDK1.1

System.getProperty返回的究竟是什么路径

1、利用System.getProperty()函数获取当前路径:

System.out.println(System.getProperty(“user.dir”));//user.dir指定了当前的路径

2、使用File提供的函数获取当前路径:

File directory= new File(“”);//设定为当前文件夹

try{

System.out.println(directory.getCanonicalPath());//获取标准的路径

System.out.println(directory.getAbsolutePath());//获取绝对路径

}catch(Exceptin e){}

File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(“.”)和new File(“..”)两种路径有所区别。

#对于getCanonicalPath()函数,“.”就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹

#对于getAbsolutePath()函数,则不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径

#至于getPath()函数,得到的只是你在new File()时设定的路径

比如当前的路径为 C:\test:

File directory= new File(“abc”);

directory.getCanonicalPath();//得到的是C:\test\abc

directory.getAbsolutePath();//得到的是C:\test\abc

direcotry.getPath();//得到的是abc

File directory= new File(“.”);

directory.getCanonicalPath();//得到的是C:\test

directory.getAbsolutePath();//得到的是C:\test\.

direcotry.getPath();//得到的是.

File directory= new File(“..”);

directory.getCanonicalPath();//得到的是C:\

directory.getAbsolutePath();//得到的是C:\test\..

direcotry.getPath();//得到的是..

J*a中getAbsolutePath()什么意思

参见下面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同样的代码得到不同的结果。

本文链接:http://www.cqlmys.com/html/87967708.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。