Pathutil.getclasspathfile ("test.txt")//So you can find
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;
public enum Pathutil {
INSTANCE;
private static String Webrootpath;
/**
* Specify the compilation path for the class
* @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 ();
}
/**
* Project Compilation Path
* @return
*/
public static String Getclasspath () {
String path = PathUtil.class.getClassLoader (). GetResource (""). GetPath ();
return new File (path). GetAbsolutePath ();
}
/**
* Specifies the package to which the object belongs
* @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);
}
/**
* Absolute path to file
* @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")); Relative path
}
}
Java file read and write, find the location of the file deployment