Java reads the properties file and java reads properties
1. Not read from the project
Properties properties = new Properties ();
BufferedReader read = new BufferedReader (new InputStreamReader (new FileInputStream ("file path"), "UTF-8 "));
Properties. load (read );
Properties. getProperty ("the key of the file") 2. Read InputStream inputStream = the class name of your tool class in the project. class. getResourceAsStream ("/file name. properties ");
Properties p = new Properties ();
Try {
P. load (inputStream );
InputStream. close ();
} Catch (IOException e1 ){
E1.printStackTrace ();
} All get value through p. getProperty (key in the configuration file)
Java reads the properties File
InputStream in = getProperties. class. getClassLoader (). getResourceAsStream (
"Config. properties ");
Try another way:
Properties props = new Properties ();
String url = this. getClass (). getClassLoader (). getResource (
"Config. properties"). toString (). substring (6 );
String empUrl = url. replace ("% 20", ""); // if your file path contains spaces, an error is returned.
System. out. println (empUrl );
InputStream in = null;
Try {
In = new BufferedInputStream (new FileInputStream (empUrl ));
Props. load (in );
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
That's what I keep writing. No problem.
I guess the reason why you read the file is empty is that there are spaces in your file path.
Java reads the properties File
InputStream in = getProperties. class. getClassLoader (). getResourceAsStream (
"Config. properties ");
Try another way:
Properties props = new Properties ();
String url = this. getClass (). getClassLoader (). getResource (
"Config. properties"). toString (). substring (6 );
String empUrl = url. replace ("% 20", ""); // if your file path contains spaces, an error is returned.
System. out. println (empUrl );
InputStream in = null;
Try {
In = new BufferedInputStream (new FileInputStream (empUrl ));
Props. load (in );
} Catch (FileNotFoundException e1 ){
E1.printStackTrace ();
} Catch (IOException e1 ){
E1.printStackTrace ();
}
That's what I keep writing. No problem.
I guess the reason why you read the file is empty is that there are spaces in your file path.