Last active
August 29, 2015 14:11
-
-
Save dalmat36/65d855a892e892387068 to your computer and use it in GitHub Desktop.
Java-IO-Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.javademo; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.util.Properties; | |
| public class PropertiesDemo { | |
| public static void main(String[] args) { | |
| //Read properties file | |
| Properties p = loadPropertiesFile("props.properties"); | |
| //Print out properties | |
| System.out.println("Username: " + p.getProperty("prop.user")); | |
| System.out.println("Password: " +p.getProperty("prop.pass")); | |
| } | |
| public static Properties loadPropertiesFile(String s) { | |
| Properties properties = new Properties(); | |
| FileReader input = null; | |
| try { | |
| input = new FileReader(s); | |
| try { | |
| properties.load(input); | |
| } catch (IOException e) { | |
| System.err.println("Failed loading properties: could not read file"); | |
| } | |
| } catch (FileNotFoundException e) { | |
| System.err.println("Failed loading properties: file not found"); | |
| } finally{ | |
| if(input!=null){ | |
| try { | |
| input.close(); | |
| } catch (IOException e) { | |
| System.err.println("Failed close properties file"); | |
| } | |
| } | |
| } | |
| return properties; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Simple Properties File | |
| prop.user=username | |
| prop.pass=password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment