J.E.D.I.
5 Text-Based Applications
5.1 Objectives
In this lesson, a review on using command-line arguments is found in this section. Moreover, you will learn more about using streams to get input from the user during
runtime and to manipulate files.
After completing this lesson, you should be able to: 1. Get input from the command-line
2. Explain how to manipulate system properties 2. Read from the standard input
4. Read and write to files
5.2 Command-Line Arguments and System Properties
As you have seen before in your previous programming course, java allows user to input data from the command line. For instance, to pass the arguments 1 and 2 to the java
program named Calculate, you can type the following line on the command prompt:
java Calculate 1 2 In this example, the data 1 is stored in the variable args[0] whereas the data 2 is stored
in args[1]. With this, the purpose of declaring String args[] as a parameter in the main method becomes clear.
If youre using NetBeans, here are the steps for passing command-line arguments. 1. Look for the project name in the Projects panel on the left. Right click on the
project name or icon. 2. Select Properties. This is the last option of the drop-down menu.
3. Click on Run from the Categories panel on the left. 4. Enter the arguments separated by space for Arguments text field.
5. Click on OK button. 6. Compile and run the program as usual.
To understand these steps more easily, the following screen shots are provided:
Introduction to Programming II Page 69
J.E.D.I.
Figure 5.1: Passing Command-line Arguments with NetBeans – Steps 1 2
Introduction to Programming II Page 70
J.E.D.I.
Figure 5.2: Passing Command-line Arguments with NetBeans – Step 3
Introduction to Programming II Page 71
J.E.D.I.
Figure 5.3: Passing Command-line Arguments with NetBeans – Step 4
Introduction to Programming II Page 72
J.E.D.I.
Besides passing arguments to the main method, you can also manipulate system properties from the command line.
System properties are quite similar to environment variables but the former not being platform-dependent. A property is simply a mapping between the property name to its
corresponding value. This is represented in Java with the Properties class. The System class provides a methods for determining the current system properties, the
getProperties method that returns a Properties object. The same class also provides for the getProperty method that has two forms.
public static String getPropertyString key This version returns string value of the system property indicated by the specified key.
It returns null if there is no property with the specified key. public static String getPropertyString key, String def
This version also returns string value of the system property indicated by the specified key. It returns def, a default value, if there is no property with the specified key.
Table 14: getProperty methods of class System
We will no longer dwell on the details of system properties but go straight to manipulating system properties applied. If you are interested in learning more about
system properties, you can refer to the API documentation.
You can use the -D option with the java command from the command-line to include a new property.
java -Dname=value For example, to set the system property user.home to have a value of philippines, use
the following command: java -Duser.home=philippines
To display the list of system properties available in your system and their corresponding values, you can use the getProperties method as follows:
System.getProperties.listSystem.out; Here is a sample list of system properties:
-- listing properties -- java.runtime.name=JavaTM 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_06\jre... java.vm.version=1.5.0_06-b05
java.vm.vendor=Sun Microsystems Inc. java.vendor.url=http:java.sun.com
path.separator=; java.vm.name=Java HotSpotTM Client VM
file.encoding.pkg=sun.io user.country=US
sun.os.patch.level=Service Pack 2 java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\Documents and Settings\becca\Neste... java.runtime.version=1.5.0_06-b05
Introduction to Programming II Page 73
J.E.D.I.
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_06\jre...
os.arch=x86 java.io.tmpdir=C:\DOCUME~1\becca\LOCALS~1\Temp\
line.separator=
java.vm.specification.vendor=Sun Microsystems Inc. user.variant=
os.name=Windows XP sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.5.0_06\jre... java.specification.name=Java Platform API Specification
java.class.version=49.0 sun.management.compiler=HotSpot Client Compiler
os.version=5.1 user.home=C:\Documents and Settings\becca
user.timezone= java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252 java.specification.version=1.5
user.name=becca java.class.path=C:\Documents and Settings\becca\Neste...
java.vm.specification.version=1.0 sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.5.0_06\jre java.specification.vendor=Sun Microsystems Inc.
user.language=en awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing java.version=1.5.0_06
java.ext.dirs=C:\Program Files\Java\jdk1.5.0_06\jre... sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_06\jre...
java.vendor=Sun Microsystems Inc. file.separator=\
java.vendor.url.bug=http:java.sun.comcgi-binbugreport... sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle sun.desktop=windows
sun.cpu.isalist=
5.3 Reading from Standard Input