Java Read Properties configuration file

Source: Internet
Author: User

Method One, use the Bufferedinputstream in the IO package and FileInputStream to read the file into a character stream, and then read it using the Load method of Properties in the Lang packet.

Package com.yli.dbutil;
Import java.io.*;
Import java.util.*;
public class Test {
public static void Main (String [] args) {
Properties Pro=new properties ();
try {
InputStream in=new Bufferedinputstream (New FileInputStream ("F:/ws/testdb/src/jdbc.properties"));
Pro.load (in);
if (pro!=null) {
System.out.println (Pro.getproperty ("Driver"));
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

Method Two: Get the class loader using the class file of this class and then convert the properties file to a stream, then load it using the Load method in the Lang packet and read it using the GetProperty method.

This method is commonly used

Package com.yli.dbutil;
Import java.io.*;
Import java.util.*;
public class Test {
public static void Main (String [] args) {
Properties Pro=new properties ();
try {
Pro.load (Test.class.getClassLoader (). getResourceAsStream ("jdbc.properties"));
if (pro!=null) {
String driver=pro.getproperty ("Driver");
SYSTEM.OUT.PRINTLN (driver);
}
} catch (IOException e) {
E.printstacktrace ();
}


}
}

Java Read Properties configuration file

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.