Create a new configuration file in the SRC directory test.properties
Format is: Key=value
Content is: Name=louis
Read Mode 1:
Public Static voidPROPS1 ()throwsException {//failed to debug, file not found//Reader read = new FileReader ("/db.properties"); //successful, the default is to find the relative path, if plus/, start from the root directory to find//InputStream read = PropertiesDemo.class.getResourceAsStream ("/db.properties"); //success, starting from the root directoryInputStream read = Propertiesdemo.class. getClassLoader (). getResourceAsStream ("Db.properties"); Properties Props=NewProperties (); Props.load (read); String s= Props.getproperty ("Driver"); System.out.println (s); }
Read Mode 2:
Public Static void props2 () { // do not Add. Properties ResourceBundle RB = Resourcebundle.getbundle ("DB" ); = rb.getstring ("Driver"); System.out.println (d); }
In the configuration file, Chinese will automatically be converted to Unicode code, you can set the default encoding to Utf-8
Using a configuration file, you can write some data to a configuration file, such as data from a database connection.
Java Read configuration file