Timothy Lee
Timothy Lee

Reputation: 59

Creating my own properties

I am unsure on how to create more than 2 properties, I used the setProperty() method and when I put more than 2 properties, NetBeans threw out a syntax error saying that the setProperty() method can only have 2 properties. Here is the code that I have so far:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class MyOwnProject {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    FileInputStream propFile = null;
    Properties p = null;

    // set up new properties object
    // from file "myProperties.txt"
    try {
        propFile = new FileInputStream(
                "myProperties.txt");
        p = new Properties(System.getProperties());
        p.load(propFile);
    } catch (IOException e) {
        System.out.println(e);
    }

    // set a property through setProperty() method

p.setProperty("mykey20" , "mykey30" , "mykey40");

    // set the system properties
    System.setProperties(p);

    // display new properties
    System.getProperties().list(System.out);
}
}

Is there any way that I can fix this? All help will be greatly appreciated.

Upvotes: 0

Views: 146

Answers (2)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

A property is a key-value pair, with emphasis on pair. It is not clear to me what you're trying to do with three elements, but it could be as simple as you rather doing this

p.setProperty( "mykey20" , "somevalue20" );
p.setProperty( "mykey30" , "somevalue30" );
...

Cheers,

Upvotes: 2

DaveH
DaveH

Reputation: 7335

setProperty takes 2 arguments - a name and a value.

p.setProperty("Name","Dave") puts the value "Dave" in to the property "Name". ( which can subsequently be retrieved via p.getProperty("Name"))

I think you'd need three separate setProperty statements to achieve what you are trying to do ( and you need to give each property a unque name in order to be able to retrieve them )

Upvotes: 1

Related Questions