Summary of Java file paths on different platforms

Source: Internet
Author: User

Usually writeProgramIn most cases, the system prompts that the file cannot be found and throws an exception.

 

Obtain a relative path
Note: You can obtain the relative path (that is, if you do not specify the relative path) in the following ways (whether it is a general Java project or a Web project)
String relativelypath =System. getproperty ("user. dir ");
In the above relative path, the files in the Java project are relative to the root directory of the project.
The file paths in the web project vary depending on the Web server (Tomcat is relative to the tomcat installation directory \ bin)

obtain the directory for loading two types (that is, obtain the directory for loading when a class is running)
1.1) general method 1 (for general Java projects or web projects, first locate the first-level directory where the package path is visible)
inputstream is = testaction. class. getclassloader (). getresourceasstream ("test.txt");
(test.txt file path: Project name \ SRC \ test.txt; Class testaction package's first-level directory is located in the src directory)
replace testactionand test.txt with the corresponding class name and file name.

1.2) general method 2 (this method is similar to the method in 1.1. The difference is that this method must start)
Inputstream is = test1.class. getresourceasstream ("/test.txt ");
(Test.txt file path is project name \ SRC \ test.txt, the first level directory of the class test1 package is located in the src directory)

3. Obtain the root directory of the WEB Project (after release)
1. Starting from Servlet

You can create a Servlet and write the following statement in its init method:
Servletcontext S1 = This. getservletcontext ();
String temp = s1.getrealpath ("/"); (key)
Result format: D: \ tool \ tomcat-6.0 \ webapps \ 002_ext \ (002_ext is the project name)

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

2. Start from httpservletrequest

String cp11111 =Request. getsession (). getservletcontext (). getrealpath ("/");

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

 

 

4. Obtain the classpath (in eclipse, obtain the path of the SRC or classes directory)

Method 1Thread. 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 2Jdomparse. Class. getclassloader (). getresource (""). getpath ()(Jdomparse is the class in a certain SRC package, the same below)

Eg: String p1 = jdomparse. Class. getclassloader (). getresource (""). getpath ();
System. Out. println ("jdomparse. Class. getclassloader (). getresource --" + p1 );

Outputs: 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 obtain the file in the following ways (first locate the last level directory 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 JDOM package under the src directory)

 

Read four attribute files:

Method 1

Inputstream in = lnew bufferedinputstream ( New Fileinputstream (name ));Properties P =New Properties ();P. Load (in );

 

Pay attention to the path issue. After execution, you can call P. getproperty ("name") to obtain the value of the corresponding attribute.

 

Method 2

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

The propertiestest. properties file in the src directory of the project contains the following content:

Test = Hello word

Related Article

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.