Java Find Files directory

Source: Internet
Author: User

Acquisition of a relative path
Description: The relative path (that is, who does not specify when it is relative) can be obtained in the following ways (whether it is a generic Java project or a Web project)
String relativelypath=system.getproperty ("User.dir");
In the relative path above, the files in the Java project are relative to the project's root directory
File paths in Web projects vary depending on the Web server (tomcat is relative to the Tomcat installation directory \ Bin)


The acquisition of a Class II loaded directory (that is, when a class of runtime obtains its loading directory)
1.1) Common method One (whether it is a generic Java project or a Web project, first navigate to the first level directory where you can see the package path)

InputStream Is=testaction.class.getclassloader (). getResourceAsStream ("test.txt");
(The path to the Test.txt file is the project name \src\test.txt; The first-level directory of the class Testaction package is located in the SRC directory)

Replace the testaction,test.txt with the corresponding class name and file name.

1.2) General method Two (this method is similar to the method in 1.1, the difference is that this method must start with '/', refer to http://riddickbryant.iteye.com/blog/436693)
InputStream Is=test1.class.getresourceasstream ("/test.txt");
(The path to the Test.txt file is the project name \src\test.txt, and the first-level directory of the class Test1 package is located in the SRC directory)




Three Web project root access (after publishing)
1 departing from the servlet

You can create a servlet that writes the following statement in its Init method
ServletContext S1=this.getservletcontext ();
String Temp=s1.getrealpath ("/"); Key
Result shape: D:\ tool \tomcat-6.0\webapps\002_ext\ (002_ext for project name)

If S1.getrealpath ("") is called, the output D:\ tool \tomcat-6.0\webapps\002_ext (less a "\")

2 Departure from HttpServletRequest

String cp11111=request.getsession (). Getservletcontext (). Getrealpath ("/");

Result shape: D:\ tool \tomcat-6.0\webapps\002_ext\

Acquisition of four classpath (the path to get SRC or classes directories in Eclipse)

Method one Thread.CurrentThread (). Getcontextclassloader (). GetResource (""). GetPath ()

Eg:string T=thread.currentthread (). Getcontextclassloader (). GetResource (""). GetPath ();
SYSTEM.OUT.PRINTLN ("t---" +t);

Output: T---/e:/order/002_ext/webroot/web-inf/classes/

Method two JdomParse.class.getClassLoader (). GetResource (""). GetPath () (Jdomparse is a class in the SRC package, same as below)

Eg:string P1=jdomparse.class.getclassloader (). GetResource (""). GetPath ();
System.out.println ("JdomParse.class.getClassLoader (). getresource--" +P1);

Output: JdomParse.class.getClassLoader (). getresource--/e:/order/002_ext/webroot/web-inf/classes/

In addition, if you want to put the file in a package, you can get to the file in the following way (navigate to the last level of the package)

eg String p2=jdomparse.class.getresource (""). GetPath ();
System.out.println ("JdomParse.class.getResource---" +p2);

Output: JdomParse.class.getResource---/e:/order/002_ext/webroot/web-inf/classes/jdom/(jdomparse is the class in the SRC Directory jdom package)

Read the four properties file:

Method One

InputStream in = Lnew bufferedinputstream ( new FileInputStream (name));   Properties P = new properties (); P.load (in);

Notice the problem of the path, after doing the execution can call P.getproperty ("name") to get the value of the corresponding property

Method Two

Locale locale = Locale.getdefault ();
ResourceBundle Localresource = Resourcebundle.getbundle ("test/propertiestest", locale);
String value = localresource.getstring ("test");
System.out.println ("ResourceBundle:" + value);

Project SRC directory under propertiestest.properties (name suffix must be properties) file content is as follows:

Test=hello Word

Java Find Files directory

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.