简体   繁体   中英

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.

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,

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM