Java_file (2)

Source: Internet
Author: User

Several less secure:

1. New File (path), where the path to the method depends on where the starting position of the calling Java command is defined,

Tomcat/bin the following catalina.bat calls Java, so the relative starting position under Tomcat is Tomcat/bin, but when Eclipse starts, the start location is the project path for Eclipse.

2. Class. Class.getclassloader (). GetResource (""). GetPath ()

If this method is used, the decision is given to the ClassLoader, such as Tomcat's class loading is a non-commissioned mechanism, and the WebLogic class load is delegated. Most of the cases are safe.

3. Class. Class.getResource ("")

This is a good way, but the limitation is that if the class file in the jar, then in the jar package will need to hit the folder together, otherwise it will return the Null,jar file is actually a zip file, zip file: File is a file, folder is a folder, not linked together, A lot of open-source jar packages don't hit the directory. Only hit the classes file, although you can go to the directory hierarchy, but call the class. Class.getResource ("") returns NULL. Because the directory structure of the file and the folder itself are two different things.

*************************************************************************************************************** ****************

There are two types of paths used in Java: absolute paths and relative paths. Ultimately, Java can only use absolute paths to find resources in essence. All the relative paths to find resources are just a few convenient ways. It's just the API that helps us build an absolute path to find resources!

 When developing Web-related applications, it is often necessary to obtain the physical path of the current Webroot in the server. In the case of Servlet, Action, Controller, or filter, Listener, interceptor and other related classes, we only need to get servletcontext, and then through Servletcontext.getrealpath ("  /") to get the physical address of the current app on the server. If ServletContext is not available in the class, there are two ways to do it: 1) Take advantage of Java's class loading mechanism: Call XXX.class.getClassLoader (). GetResource ("");  method to get to Classpath, and then process to get the Webroot directory. This can only take effect if the class is webroot/web-inf/classes, and if the class is packaged in a jar file, the method fails.  This should be done in one of the following ways. 2) Spring framework idea, in Web-inf/web.xml, create a webapprootkey param, specify a value (default = Webapp.root) as the key value, and then through listener, or filter, Or the servlet executes string webapprootkey = Getservletcontext (). Getrealpath ("/"); The webapprootkey corresponding webapp.root are written to the system properties as Key,value respectively.  The physical path of the Webroot is then obtained through System.getproperty ("Webapp.root") in the program. According to the second way of thinking, we can also expand a bit.  But for applications that are deployed on a single server, look down if it's not what you need. Here are some ways to get the absolute path of classpath and the current class. You can use some of these methods to get the absolute path of the resource you need: 1.debitnoteaction.class.getresource ("") gets the URI directory of the current class Filetest.class file.  Don't include yourself! such as: File:/d:/eclipse/springtest/webroot/web-inf/classes/atacarnet/src/com/evi/modules/atacarneT/action/2.debitnoteaction.class.getresource ("/") gets the absolute URI path of the current classpath. such as: File:/d:/eclipse/springtest/webroot/web-inf/classes/3.thread.currentthread (). Getcontextclassloader ().  GetResource ("") is also the absolute URI path for the current classpath, such as: file:/d:/eclipse/springtest/webroot/web-inf/classes/is recommended for use with this method.  4.debitnoteaction.class.getclassloader (). GetResource ("") or Classloader.getsystemresource ("")  The resulting absolute URI path is also the current classpath. such as: FILE:/D:/ECLIPSE/SPRINGTEST/WEBROOT/WEB-INF/CLASSES/5. Get the server relative path System.getproperty ("User.dir") For example: E:\ Apache-tomcat-5.5.16\apache-tomcat-5.5.16\bin 6. The absolute path obtained in the project is generally request.getrealpath ("/") or Request.getrealpath ("/  config/") But now does not advocate the use of Request.getrealpath ("/"), you can try the Servletcontext.getrealpath ("/") method to get the absolute path of the root directory of the Web application.  To get the src file is very easy, because SRC is the default relative directory, such as you say to get src under the COM directory of the Test.java file, you only need this is enough for file F = new file (Com/test.java);  But if I'm going to get a file that's not in the SRC directory or the Webroot directory, I'm going to take it from a directory like SRC or webroot, such as Doc.  My hard method is implemented like this: String path = This.getservletcontext (). Getrealpath ("/"); Properties p = new PrOperties ();  P.load (New FileInputStream (New File (path.substring (0, (Path.lastindexof ("\\WebRoot") + 1) + "doc/db.properties")); System.out.println (P.getproperty ("drivername"));-------------------------------another: request in Getcontextpath, Getservletpath, Getrequesturi, Getrequesturl, getrealpath the difference assumes your Web application name is news, you enter the request path in the browser:/http  LOCALHOST:8080/NEWS/MAIN/LIST.JSP executes the following line code to print out the following results: 1, System.out.println (Request.getcontextpath ());  Printing results:/news2, System.out.println (Request.getservletpath ());  Printing results:/MAIN/LIST.JSP3, System.out.println (Request.getrequesturi ());  Printing results:/NEWS/MAIN/LIST.JSP4, System.out.println (Request.getrequesturl ());  Print results: HTTP://LOCALHOST:8080/NEWS/MAIN/LIST.JSP5, System.out.println (Request.getrealpath ("/")); Print Result: F:\Tomcat 6.0\webapps\news\test

Java_file (2)

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.