I/O Fundamentals in JAVA 1
Command Line Arguments:-
- When a Java technology program is launched from a terminal window, we can provide the program with zero or more command line arguments.
- Those command line arguments allow the user to specify the configuration information for the application.
- These arguments are strings; either standalone tokens, such as arg1, or quoted strings, such as “another arg”.
- The sequence of arguments follows the name of the program class and is stored in an array of String objects passed to the static main method.
- The code given below is to test the command line arguments,
- public class TestArgs
- {
- public static void main(String args[])
- {
- for(int i = 0; i < args.length; i++)
- {
- System.out.println(“args[” + i + ”] is: “ + args[i]);
- }
- }
- }
- In this case, this program displays each command line argument that is passed to the TestArgs program. For example,
java TestArgs arg1 arg2 “another arg”
args[0] is: arg1
args[1] is: arg2
args[2] is: another arg
Note:- If an application requires command line arguments other that type String, for example numeric values, the application should convert the String arguments to their respective types using the Wrapper classes, such as Integer.parseInt method, which can be used to convert the String argument that represents the numeric integer to type int.
System Properties:-
- System properties are another mechanism used to parameterize a program at run time.
- A property is a mapping between a property name and its value; both strings. The Properties class represents this kind of mapping.
- The System.getProperties method returns the system properties object.
- The System.getProperty(String) method returns the string value of the property named in the String parameter.
- There is another method, System.getProperty(String, String), that enables we to supply a default string value (the second parameter), which is returned if a named property does not exist.
Note:- every JVM implementation must supply a default set of properties. Moreover, a particular JVM implementation vendor can supply others.
- There are also static methods in the wrapper classes that perform conversion of property values: Boolean.getBoolean(String), Integer.getInteger(String), and Long.getLong(String).
- The string argument is the name of the property. If the property doen not exist, then false or null (respectively) is returned.
The Properties class:-
- An object of the Properties class contains a mapping between property names(String) and values(String).
- It has two main methods for retrieving a property value: getProperty(String) and getProperty(String, String).
- The latter method provides the capability of specifying a default value that is returned if the named property does not exist.
- We can iterate through the complete set of property names using the propertNames method.
- By calling getProperty on each name, we can retrieve all of the values.
- Finally, property sets can be stored and retrieved from any I/O stream using the store and load methods.
- The program given below lists the complete set of the properties that exist when the program executes:
- import java.util.Properties;
- public class TestProperties
- {
- public static void main(String args[])
- {
- Properties props = System.getProperties();
- props.list(System.out);
- }
- }
- Line 7 retrieves the set of system properties and Line 8 prints the properties using the list method of the Properties class.
java –DmyProp=theValue TestProperties
For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.