Problems with file read and relative paths in Java

Source: Internet
Author: User

Content transferred from: 53900636

1 New Properties (); 2 InputStream is = dbutils. class. getResourceAsStream ("jdbc.properties");

Find files from the class root directory as follows

1 New Properties (); 2 inputstream instream= dbutil. class. getClassLoader (). getResourceAsStream ("configjdbc.properties"); 3 properties.load (instream);

Here's how to find files from the package under the current class

1 New Properties (); 2 inputstream instream= dbutil. class. getResourceAsStream ("configjdbc.properties"); 3 properties.load (instream);

==================== following from reprint =============

Introducing code from a database configuration file in Java, thinking about how and how files are loaded

  

One, for the Java project to read the file

1. Use System or the Properties object

① is directly using  String relativelypath=system.getproperty ("User.dir");
② using the Properties object, let's go through the system's attributes: Property properties = System.getproperties ();
Enumeration pnames = Properties.propertynames ();
while (Pnames.hasmoreelements ()) {
string pname = (string) pnames.nextelement ();
System.out.print (pname + "--------------");
System.out.println (Properties.getproperty (pname));
}

Cut a picture for everyone:

This is the property of the system, which is actually going around to using the User.dir property to get the true path of the current project

by String Relativelypath = Properties.getproperty ("User.dir");

My own computer above the project Log4jproj the True path is:

User.dir--------------D:\Develop\workspace\ws_self\10_ws_eclipse_j2ee_mars\Log4jProj

In fact the way ① and way ② a meaning, the same

2, the second way: Use the class loader of the current class to get ClassLoader

First of all, how to get a class byte code instance, three ways: (for example, my class called demo)

①demo.class

②class.forname ("Full name of the class")

③ uses the instance object of the demo to invoke the object's GetClass () method to get the class instance of the object

After reviewing how to get the class bytecode instance, then review how to get the ClassLoader object

①demo.class.getclassloader ()

②class.forname ("Full name of the class"). getClassLoader ()

③ assumes that the demo is an instantiated object Demo.getclass (). getClassLoader ()

④ through the Thread object's Getcontextclassloader () method to get

Thread.CurrentThread (). Getcontextclassloader ()

Go to the Chase:

With the ClassLoader object, we can get the files in the Java project at this time through the ClassLoader object

First of all, let's see my current project directory structure

and the directory structure of the actual files

The requirement is that at this point test needs to read the path to the Log4j.properties file

Two ways to use ClassLoader, one static, one non-static

Output Result:

Remember Oh, here the Getsystemresource method gets the URL object and needs to call the GetPath () method to get the path

1, when just get log4j.properties file input stream can be in the following two ways

① is still using ClassLoader, which has two methods, both of which are static and a non-static

Classloader.getsystemresourceasstream ("Config/log4j.properties");

Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("config/log4j.properties");

② the file file before wrapping, and then creating a new input stream

File FILE01 = new file ("Config/log4j.properties");
System.out.println (File01.getabsolutepath ());

File FILE02 = new file (Properties.getproperty ("User.dir") + "/bin/config/log4j.properties");
System.out.println (File02.getabsolutepath ());

Classloader.getsystemresource Gets the URL object
File file03 = new file (Classloader.getsystemresource ("Config/log4j.properties"). GetPath ());
System.out.println (File03.getabsolutepath ());

The way in which FILE03 is created is not recommended, because the Getsystemresource method is not available if the file is not obtained.

The URL object is null, and calling GetPath () at this time will cause an error

If you have a file object, you can create the stream directly, and not repeat it here.

Problems with file read and relative paths in Java

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.