Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

Java Tutorial
Java Introduction
Object Oriented Programming Concepts
Anatomy of a Java Application
Syntax and Semantics of Java
Java Objects, Classes, and Interfaces
The String and StringBuffer Classes in Java
Setting Program Attributes in Java
Using System Resources in Java
Threads of Control in Java
Errors and Exceptions in Java
Java Input and Output Streams
Overview of Java Applet
Creating an Applet User Interface in Java
Communicating with Other Programs in Java
Overview of the Java UI
Using GUI Building Blocks in Java
Laying Out Components within a Container
Working with Graphics in Java
How Java Differs from C and C++
Java Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


Setting Program Attributes in Java

Previous Next



Application attributes are often called preferences and can often allow the user to configure various start up options, preferred window size, or whatever.




Setting Program Attributes

A program typically needs this information about the system and application environment to make decisions about how to do something, or what to do. Also, a program may wish to modify certain attributes itself, or allow a user to change them. Thus a program needs to be able to read and write various system attributes and program-specific attributes. Java programs can manage these attributes through three mechanisms: properties, application command line arguments, and applet parameters.

  • Properties
    Use properties to define environmental attributes on a persistent basis. That is, use properties when they need to be in effect for each invocation of a program.
  • Application Command Line Arguments
    Use command line arguments to define or modify environmental attributes on a non-persistent basis. That is, use command line arguments to change one or more attributes for just one invocation of a program.
  • Applet Parameters
    Use applet parameters to define or modify environmental attributes on a non-persistent basis for applets. That is, use parameters to set one or more attributes for a single invocation of an applet.



Converting Objects to Strings

You can use the Properties class from java.util to manage attributes specific to your Java programs. A Properties object manages a set of key/value pairs: the key represents the name of a property and the value is the current value of the property. You can load key/value pairs into a Properties object from a stream, save the properties to a stream, and get information about the properties represented by the Properties object.




Setting up Your Properties Object

Often when a program starts up, it will use code similar to the following to set up the properties object:

// set up default properties
Properties defaultProps = new Properties();
FileInputStream defaultStream = new FileInputStream("defaultProperties");
defaultProps.load(defaultStream);
defaultsStream.close();
// set up real properties
Properties applicationProps = new Properties(defaultProps);
FileInputStream appStream = new FileInputStream("appProperties");
applicationProps.load(appStream);
appStream.close();

First the application sets up a default Properties object. This object contains the set of properties to use if values are not explicitly set elsewhere. This code snippet uses the load() method to read the default values from a file on disk named defaultProperties. Applications usually save and restore properties to files on the disk.

Next, the program uses a different constructor to create a second Properties object, applicationProps. This object uses defaultProps to provide its default values. Then the code snippet loads a set of properties into applicationProps from a file named appProperties. The properties loaded into appProperties can be set on a per user basis, a per site basis, or whatever is appropriate for the current application.




Getting Property Information

Once you've set up your Properties object, you can query it for information about various properties it contains. The Properties class provides several methods for getting property information

getProperty() (2 versions)

returns the value for the specified property. One version allows you to provide a default value--if the key is not found the default is returned.

list()

writes all the properties to the specified stream. This is useful for debugging.

propertyNames()

returns an Enumeration containing all of the keys contained in the Properties object.




Command Line Arguments

Your Java application can accept any number of arguments from the command line. Command line arguments allow the user to affect the operation of an application. For example, a program might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command line argument -verbose.

When invoking an application, the user types the command line arguments after the application name. For example, suppose you had a Java application, called Sort, that sorted lines in a file, and that the data you want sorted is in a file named ListOfFriends. If you were using DOS, you would invoke the Sort application on your data file like this:

C:\> java Sort ListOfFriends

Echo Command Line Arguments
This simple application displays each of its command line arguments on a line by itself:

class Echo
{
public static void main (String args[])
{
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}

Try this: Invoke the Echo application with the command line shown in this DOS example:

C:\> java Echo Drink Hot Java Drink Hot Java

You'll notice that the application displays each word--Drink, Hot, and Java--on a line by itself. This is because The Space Character Separates Command Line Arguments.




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: Java Methods, Java Dynamic Data (JDD) Classes, Java Extension Mechanism


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.