Java如何获取文件路径



Java如何获取文件路径? ClassPath的绝对URI路径,工程的路径。

1、实例说明

(1)java得到 ClassPath的绝对URI路径:

Thread.currentThread().getContextClassLoader().getResource(“.”).getPath();

(2)java得到 ClassPath的绝对URI路径:

Thread.currentThread().getContextClassLoader().getResource(“”).getPath();

(3)java得到工程的路径

System.getProperty(“user.dir”);


2、实例源码

/**
* @Title:FilePath.java
* @Package:com.you.model
* @Description:
* @Author: 游海东
* @date: 2014年3月29日 上午11:42:16
* @Version V1.2.3
*/
package com.you.model;

/**
* @类名:FilePath
* @描述:
* @date: 2014年3月29日 上午11:42:16
*/
public class FilePath {

/**
* @Title : main
* @Type : FilePath
* @date : 2014年3月29日 上午11:42:17
* @Description :
* @param args
*/
public static void main(String[] args)
{
/**
* 得到 ClassPath的绝对URI路径
*/
<span style=”white-space:pre”> </span>String str1 = Thread.currentThread().getContextClassLoader().getResource(“.”).getPath();
<span style=”white-space:pre”> </span> /**
<span style=”white-space:pre”> </span> * 得到 ClassPath的绝对URI路径
<span style=”white-space:pre”> </span> */
<span style=”white-space:pre”> </span>String str2 = Thread.currentThread().getContextClassLoader().getResource(“”).getPath();
<span style=”white-space:pre”> </span> /**
<span style=”white-space:pre”> </span> * 得到工程的路径
<span style=”white-space:pre”> </span> */
<span style=”white-space:pre”> </span> String str3 = System.getProperty(“user.dir”);
<span style=”white-space:pre”> </span> System.out.println(str1);
<span style=”white-space:pre”> </span> System.out.println(str2);
<span style=”white-space:pre”> </span> System.out.println(str3);
}

}

3、java获取文件路径实例结果

/D:/eclipse/workspace/HighCharts/build/classes/
/D:/eclipse/workspace/HighCharts/build/classes/
D:\eclipse\workspace\HighCharts