Java file read and write, find the location of the file deployment

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.