Java read configuration file methods and file path considerations

Source: Internet
Author: User

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

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.