Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Java reads properties files

Shulou Source: shulou.com Published: 2022-06-03 06:42:43 09月23日 Update

The file name is db.properties

The first one is obtained through ResourceBundle

ResourceBundle bunble = ResourceBundle.getBundle ("db")

Driver = bunble.getString ("driver")

Url = bunble.getString ("url")

Username = bunble.getString ("username")

Password = bunble.getString ("password")

The second is to obtain / / 1 through the class loader. Get the access method of class loader through the current class: type .class.getClassLoader () ClassLoader classLoader = JDBCUtils_V3.class.getClassLoader (); / / 2. Get an input stream InputStream is = classLoader.getResourceAsStream ("db.properties") through the class loader; / / 3. Create the properties object Properties p = new Properties (); / / 4. Load the input stream p.load (is); / / 5. Get the values of related parameters driver = p.getProperty ("driver"); url = p.getProperty ("url"); username = p.getProperty ("username"); password = p.getProperty ("password")

Tags: Input file parameter object method type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Redmi Docker Shulou Information