java 文件读写,查找文件部署的位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 文件读写,查找文件部署的位置相关的知识,希望对你有一定的参考价值。
PathUtil.getClasspathFile("test.txt") //这样就可以查找
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public enum PathUtil {
INSTANCE;
private static String webRootPath;
/**
* 指定类的编译路径
* @param clazz
* @return
*/
@SuppressWarnings("rawtypes")
public static String getPath(Class clazz) {
String path = clazz.getResource("").getPath();
return new File(path).getAbsolutePath();
}
public static String getPath(Object object) {
String path = getPath(object.getClass());
return new File(path).getAbsolutePath();
}
/**
* 项目编译路径
* @return
*/
public static String getClassPath() {
String path = PathUtil.class.getClassLoader().getResource("").getPath();
return new File(path).getAbsolutePath();
}
/**
* 指定对象所属的包
* @param object
* @return
*/
public static String getPackagePath(Object object) {
Package p = object.getClass().getPackage();
return p != null ? p.getName().replaceAll("\\.", "/") : "";
}
public static String getWebRootPath() {
return webRootPath == null ? detectWebRootPath() : webRootPath;
}
public static void setWebRootPath(String rootPath) {
if (rootPath.endsWith(File.separator)) {
rootPath = rootPath.substring(0, rootPath.length() - 1);
}
PathUtil.webRootPath = rootPath;
}
private static String detectWebRootPath() {
try {
String path = PathUtil.class.getResource("/").getFile();
return new File(path).getParentFile().getParentFile().getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static InputStream fromClassPath(String fileName) {
if (!fileName.startsWith("/"))
fileName = "/" + fileName;
return PathUtil.class.getResourceAsStream(fileName);
}
public static InputStream fromJar(String fileName) {
return PathUtil.class.getClassLoader().getResourceAsStream(fileName);
}
/**
* 文件的绝对路径
* @param path
* @return
*/
public static String toAbsolutePath(String path) {
if (StringUtil.empty(path)) {
return null;
}
File file = new File(path);
if ((!file.exists()) || (path.startsWith("classpath:"))) {
file = getClasspathFile(path);
return file.getAbsolutePath();
}
return path;
}
public static File getClasspathFile(String classpath) {
String input = classpath;
if (classpath.startsWith("classpath:")) {
input = classpath.substring(10);
}
URL url = getDefaultClassLoader().getResource(input);
if ((url == null) || (!url.getProtocol().equals("file"))) {
return null;
}
return toFile(url);
}
public static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (Throwable ex) {
}
if (cl == null) {
cl = PathUtil.class.getClassLoader();
}
return cl;
}
public static File toFile(URL url) {
if ((url == null) || (!url.getProtocol().equals("file"))) {
return null;
}
String filename = url.getFile().replace(‘/‘, File.separatorChar);
int pos = 0;
while ((pos = filename.indexOf(‘%‘, pos)) >= 0) {
if (pos + 2 < filename.length()) {
String hexStr = filename.substring(pos + 1, pos + 3);
char ch = (char) Integer.parseInt(hexStr, 16);
filename = filename.substring(0, pos) + ch + filename.substring(pos + 3);
}
}
return new File(filename);
}
public static void main(String[] args) {
System.out.println(PathUtil.getClasspathFile("test.txt")); //相对路径
}
}
以上是关于java 文件读写,查找文件部署的位置的主要内容,如果未能解决你的问题,请参考以下文章