Java reads the properties file and java reads properties

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.