简体   繁体   中英

How to read key value pair from a option file?

I want to read key value pair from a option file in my application.I want to pass the value as command line argument like debug.

In debug-

-debug "option file path"

similarly i want to do the same thing in my key value pair

Content of Option file:

com.xxx.xxx.xxx.xxx.xxx/isjubula = false

so as debug here i want to pass

-isjubula " path"

so that my application can link to this file and could read its content.

What's the problem with :

public class MyClass{
    public static void main(String args[]) {

        if(args.length == 2 && args[0].equalsIgnoreCase("-isjubula")) {
            System.out.println(args[1]); //get the option
        }
        else {
           System.out.println("Illegal options");
        }
    }
}

You can provide option to the above program, while running it in the command line like bellow:

java MyClass -isjubula "/home/myconf.conf"

or

java MyClass -isjubula /home/myconf.conf

Edit:

No, there is still no facility like this. But one thing you should know, the way of taking debug option like mentioned here , is known as instrumentation . Generally instrumentation is totally platform dependant. But ideally, you should not use something in your code that will make it solely eclipse dependant!

Well, Java's main method can help you.. Read some documentation and refer to how one can use main method arguments array.

public static void main(String args[]) {
   String commandType = args[0];
   String command = args[1];

   if(commandType.equals("-isjubula"){
         //TODO: your code here...
   }

}

I have not added any null checks here.. please add them..

You can run this type of java progrm after compiling it and running it like java TestClass "-isjubula" "hello world";

I found a better way. Please see the code below.

    //code
    import java.util.Arrays;
    import java.util.List;
    public class Test {
        public static void main(String[] args) {
            List<String> st = Arrays.asList(args);
            if (st.contains("-zipfile")) {
                if (st.indexOf((st.indexOf("-zipFile") + 1)) != -1) {
                    // TODO your code
                }
            }

            if (st.contains("-priority")) {
                if (st.indexOf((st.indexOf("-priority") + 1)) != -1) {
                    // TODO your code
                }
            }
        }
    }

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