package com.core.util;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
/**
* /**
*
*/
public class Path {
/**
* 获取一个类的class文件所在的绝对路径。 这个类可以是JDK自身的类,也可以是用户自定义的类,或者是第三方开发包里的类。 只要是在本程序中可以被加载的类,都可以定位到它的class文件的绝对路径。
*
* @param cls
* 一个对象的Class属性
* @return 这个类的class文件位置的绝对路径。 如果没有这个类的定义,则返回null。
*/
public static String getPathFromClass(Class cls) throws IOException {
String path = null;
if (cls == null) {
throw new NullPointerException();
}
URL url = getClassLocationURL(cls);
if (url != null) {
path = url.getPath();
if ("jar".equalsIgnoreCase(url.getProtocol())) {
try {
path = new URL(path).getPath();
} catch (MalformedURLException e) {
}
int location = path.indexOf("!/");
if (location != -1) {
path = path.substring(0, location);
}
}
File file = new File(path);
path = file.getCanonicalPath();
}
return path;
}
/**
* 这个方法可以通过与某个类的class文件的相对路径来获取文件或目录的绝对路径。 通常在程序中很难定位某个相对路径,特别是在B/S应用中。 通过这个方法,我们可以根据我们程序自身的类文件的位置来定位某个相对路径。 比如:某个txt文件相对于程序的Test类文件的路径是../../resource/test.txt, 那么使用本方法Path.getFullPathRelateClass("../../resource/test.txt",Test.class) 得到的结果是txt文件的在系统中的绝对路径。
*
* @param relatedPath
* 相对路径
* @param cls
* 用来定位的类
* @return 相对路径所对应的绝对路径
* @throws IOException
* 因为本方法将查询文件系统,所以可能抛出IO异常
*/
public static String getFullPathRelateClass(String relatedPath, Class cls) throws IOException {
String path = null;
if (relatedPath == null) {
throw new NullPointerException();
}
String clsPath = getPathFromClass(cls);
File clsFile = new File(clsPath);
String tempPath = clsFile.getParent() + File.separator + relatedPath;
File file = new File(tempPath);
path = file.getCanonicalPath();
return path;
}
/**
* 获取类的class文件位置的URL。这个方法是本类最基础的方法,供其它方法调用。
*/
private static URL getClassLocationURL(final Class cls) {
if (cls == null)
throw new IllegalArgumentException("null input: cls");
URL result = null;
final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
final ProtectionDomain pd = cls.getProtectionDomain();
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
if (cs != null)
result = cs.getLocation();
if (result != null) {
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip"))
result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
else if (new File(result.getFile()).isDirectory())
result = new URL(result, clsAsResource);
} catch (MalformedURLException ignore) {
}
}
}
}
if (result == null) {
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource);
}
return result;
}
public static String getWebRootPath() {
String path = "";
try {
path = getFullPathRelateClass("../../../../../", Path.class);
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
public static void main(String[] args) {
try {
Path.getWebRootPath();
System.out.println(getPathFromClass(Path.class));
System.out.println(getFullPathRelateClass("../test/abc/..", Path.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
android获取文件绝对路径类,是本人项目中直接取出的类。在protected void onActivityResult()中使用
windows linux 下,获取java项目绝对路径的方法,struts2设置了struts.multipart.saveDir后会在根目录建立文件夹,这样会涉及linux下的权限问题
NULL 博文链接:https://xnbhnly.iteye.com/blog/1109967
主要介绍了JAVA获取文件绝对路径的方法,涉及针对文件路径的操作技巧,需要的朋友可以参考下
一文讲清Python PyQt5的控件如何实现拖放获取文件路径 内部以QLineEdit为例子,从新类创建,代码理解,到融入项目均详细讲解。
QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位 QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位 QT文件夹定位 QT文件夹绝对路径 QT文件夹绝对路径定位
C#根据当前进程的启动路径,获得多个参数拼接的绝对路径。以及根据基准路径和参数获得相对路径。路径操作只用.NET中的System.IO和System.Text命名空间,相比其它的实现方式(比如采用System.Web命名空间)实用性更强...
Java中获取文件路径的几种方式。 关于绝对路径和相对路径
asp.net获取网站路径 绝对路径 相对路径
下面小编就为大家分享一篇python3中获取文件当前绝对路径的两种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
获取指定目录下所有文件的绝对路径和文件名
博客地址:http://blog.csdn.net/lowprofile_coding/article/details/48269011 1.获取扩展存储设备 2.获取sdcard2外部存储空间 3.获取可用的 EMMC 内部存储空间 4.获取其他外部存储可用空间 5.获取内部存储目录
提供ftp服务时需要获取文件绝对路径,这里记录一下。 #include #include #include int main(){ const char *file_name = filename; char abs_path_buff[PATH_MAX]; //获取文件路径, 填充到abs_path_buff //...
下面小编就为大家带来一篇js获取上传文件的绝对路径实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下面小编就为大家带来一篇JS获取input file绝对路径的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Android通过文件路径如何得到文件相关信息,如 文件名称,文件大小,创建时间,文件的相对路径,文件的绝对路径等:
首先创建一个文件,用于保存拍照图像,然后根据不同系统版本获取Uri,传递给Intent,然后调起相机(可以考虑将outputImage、imageUri设置为全局变量)。 3、处理回调 使用BitmapFactory读取imageUri,得到bitmap,...
Java 开发中我们经常要获取文件的路径,比如读取配置文件等等。今天我们就关于文件的路径和如何读取文件简单地探讨一下。 2. 文件的路径 文件的路径通常有 相对路径 与 绝对路径。 2.1 相对路径 以当前文件为基准...
dirname(__FILE__) 取到的是当前文件的绝对路径,也就是说,比起相对路径,查找速度是最快的。
文件可以获取指定目录下的所有文件的绝对路径及文件的名称,同样定义一个静态全局变量也可以统计指定目录下的总文件数目