项目中获取绝对路径

本文介绍了一种在Java中获取CLASSPATH下指定文件绝对路径的方法,并通过具体代码示例展示了如何实现这一功能。
获取CLASSPATH下文件的绝对路径
当使用相对路径写入文件时候,就需要用到绝对路径。下面是个例子:
package com.lavasoft;

import java.io.File;

/**
* CLASSPATH文件的绝对路径获取测试
*
* @author leizhimin 2010-1-18 9:33:02
*/
public class Test {
//classpath的文件路径
private static String cp = "/com/lavasoft/cfg/syscfg.properties";

public static void main(String[] args) {
//当前类的绝对路径
System.out.println(Test.class.getResource("/").getFile());
//指定CLASSPATH文件的绝对路径
System.out.println(Test.class.getResource(cp).getFile());
//指定CLASSPATH文件的绝对路径
File f = new File(Test.class.getResource(cp).getFile());
System.out.println(f.getPath());
}
}


输出:
/D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/
/D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/com/lavasoft/cfg/syscfg.properties
D:\projects\bbt\code\cdn\planrpt\out\production\planrpt\com\lavasoft\cfg\syscfg.properties

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值