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