In Java, it is often necessary to locate the location of some files, in order to make the program and physical location independent, use a relative path. But the use of relative paths in Java will always encounter some very troublesome problems, that is, relative to which reference. Because we usually use relative paths that are always relative to the current working directory, sometimes the requirements are not. For example, to use a relative path in a development package, it is not known where the development package is being invoked by other programs, and especially in Web applications, it is difficult to determine the relative path of a file throughout the application.
So the best way to use the relative path is to let the path relative to the reference is my development package or my application itself, the best is the class file in my development package. As long as you know the absolute path of a class file, you can use it as a reference object, using a relative path to locate any other file.
To implement this idea, I wrote this path class, which provides two static public methods, a location to locate class files for classes, and another to locate a relative path based on a class. Using these two methods, we can completely disregard the current working path of the application and look for any file according to our position. For example, when writing a functional development package, you can completely without the control of the application of this development package path situation, but only according to the location of the development package itself to locate the file, so good to achieve the encapsulation, the file path processing completely closed within the development package itself.
The following is the source code for the path class:
* Date Created 2004-11-22 * * Change the generated file template for * window > Preferences > Java > Code generation > Codes and annotations */package mytools;
/** * @author from the month * * This class provides a number of methods that are positioned according to the class file location of the classes. */public class Path { /** * Gets the absolute path of the class file for the classes. This class can be the JDK's own class, or it can be a user-defined class, or a class in a third-party development package. * You can navigate to the absolute path of its class file as long as it is a class that can be loaded in this program. * * @param cls * The class attribute of an object * @return The absolute path of the class file location for this category. If there is no definition for this class, NULL is returned. */ 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; }
/** * This method can obtain the absolute path of a file or directory by a relative path to the class file of a particular type. It is often difficult to locate a relative path in a program, especially in B/s applications. In this way, we can locate a relative path based on the location of the class file of our program itself. * For example: A TXT file relative to the program test class file path is ... /.. /resource/test.txt, * Then use this method Path.getfullpathrelateclass (". /.. /resource/test.txt ", Test.class) * Results are the absolute path of TXT file in the system. * * @param relatedpath * relative path * @param cls * used to locate the class * @return Absolute path of relative path * @throws ioexception * Because this method will query the file system, it may throw IO exception */ public static string Getfullpathrelateclass (String Relatedpath, Class CLS throws IOException { string path = null; if (Relatedpath = null) { throw new NullPointerException (); } string Clspath = GetPathFromClass (CLS); &nbsP File Clsfile = new file (clspath); string TempPath = clsfile.getparent () + File.separator + relatedpath; & nbsp File File = new file (temppath); path = File.getcanonicalpath (); return path; }
/** * Gets the URL of the class file location for the classes. This method is the most basic method of this class for other methods to invoke. * * 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 (); Java.lang.Class contract does not specify//if ' PD ' can ever is null; It isn't the case for Sun's implementations,//But guard against NULL//Just in case:if (PD!= null) {final C Odesource cs = Pd.getcodesource (); ' CS ' can is null depending on//the ClassLoader behavior:if (CS!= null) result = Cs.getlocation ();
if (Result!= null) { //Convert a code source location into & nbsp;//a full class file location //for some common cases: 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) {//Try to find ' CLS ' definition as a resource; This is not//document. D to is legal, but Sun ' s//implementations seem to//allow this:final = ClassLoader clsloader (); result = Clsloader!= null? Clsloader.getresource (Clsasresource): Classloader.getsystemresource (Clsasresource); return result; }
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.