Can a property from a .properties file be made into a usable variable?


  1. Posts : 21
    Windows 7 Home Premium 64bit
       #1

    Can a property from a .properties file be made into a usable variable?


    I have code to read out lines from .properties, but it just prints it out, because im an idiot and didn't notice till now. So, the question is, can it be made into a variable instead?
    current code;
    Code:
    Properties prop = new Properties();
    	InputStream input = null;
    	try {
    		input = new FileInputStream("config.properties");
    		prop.load(input);
    		System.out.println(prop.getProperty("CyclesTime"));
    		System.out.println(prop.getProperty("NmbOfCycles"));
    		System.out.println(prop.getProperty("MsgForSave"));
    		System.out.println(prop.getProperty("MsgForRestart"));}
    		catch (IOException ex) {ex.printStackTrace();}
    			finally {
    				if (input != null) {
    					try { input.close();} 
    						catch (IOException e) {e.printStackTrace();} } }
    And anything wrong with the code while I'm asking?
      My Computer


  2. Posts : 2,465
    Windows 7 Ultimate x64
       #2

    Of course, you're reading and outputting to console immediately, so it can't be of further use. Just assign it to a variable instead. Something like that will do:

    Code:
    int nmbOfCycles = (int)prop.getProperty("CyclesTime");
    System.out.println(nmbOfCycles);                         //here it prints the variable with the property value
    //further usage of the same property can be done on the variable
    Note that I have not tested it, and I don't have really many Java skills, but the idea is that, save to a variable first, then use the variable as many times as you want.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:53.
Find Us