Java中获取文件路径

来源:互联网 发布:食品流通许可证 知乎 编辑:程序博客网 时间:2024/06/05 02:27

Java中获取文件路径


1、实例说明

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

Thread.currentThread().getContextClassLoader().getResource(".").getPath();

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

Thread.currentThread().getContextClassLoader().getResource("").getPath();

(3)得到工程的路径

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 * @描述: * @Author:游海东 * @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路径 */        String str1 = Thread.currentThread().getContextClassLoader().getResource(".").getPath();        /**           * 得到 ClassPath的绝对URI路径          */        String str2 = Thread.currentThread().getContextClassLoader().getResource("").getPath();        /**           * 得到工程的路径          */        String str3 = System.getProperty("user.dir");        System.out.println(str1);        System.out.println(str2);        System.out.println(str3);}}

3、实例结果

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


1 0
原创粉丝点击