First, the method see the code in the note description, you need to note that the file path to read, different methods, the interpretation of the path is not the same.
1 Packagecom.yard39.ProperTiesFileReader.Test;2 3 ImportJava.io.FileInputStream;4 Importjava.io.FileNotFoundException;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 Importjava.util.Properties;8 9 Public classPropertiesfilereader {Ten One //Test Method A Public Static voidMain (string[] args) { -Properties p =NewProperties (); - InputStream is; the Try { -String filepath1 = "Src/config/config.properties"; -String filepath2 = "/config/config.properties"; -String filepath3 = "Config/config.properties"; + //1, using the file stream method, note that the path from the project root directory start reading, do not "/" - //is = new FileInputStream (filepath1); + //2, using the Class.getresourceasstream () method, note that the path from the SRC read, need to use "/" A //is = PropertiesFileReader.class.getResourceAsStream (filepath2); at //3, using Class.getclassloader (). getResourceAsStream (), note that the path is read from SRC, not "/" -is = Propertiesfilereader.class. getClassLoader (). getResourceAsStream (FILEPATH3); - P.load (IS); - is.close (); -String value = P.getproperty ("Jdbc.connection.username"); - System.out.println (value); in}Catch(FileNotFoundException e) { - e.printstacktrace (); to}Catch(IOException e) { + e.printstacktrace (); - } the } *}
Ii. Summary of File paths
(Post-fill)
Java read configuration file methods and file path considerations