You are on page 1of 10

 

The System class provides a Properties object that describes the configuration of the current
working environment of the system. System properties provides several information related to
the system, user or version etc. The Enumeration class generates the series of elements.

 System.getProperties()-This method determines the current system properties.

Here is the code of GetSystemProperties.java

import java.util.*;

class  GetSystemProperties{

  public static void main(Stri

ng[] args) {

  Properties prop = System.get

Properties();

  Enumeration keys = prop.keys

();

  while (keys.hasMoreElements(

)) {

  String key = (String)keys.ne

xtElement();

  String value = (String)p.get

(key);

  System.out.println(key + ": 

" + value);

 }

  }

Output will be displayed as:


Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is
a String and the value is also a String. The Properties class is used by many other Java
classes. For example, it is the type of object returned by System.getProperties() when
obtaining environmental values.
Properties defines the following instance variable:

Properties defaults;

This variable holds a default property list associated with


a Properties object. Properties defines these constructors:
Properties( ) 
Properties(Properties propDefault)
The first version creates a Properties object that has no default values. The second
creates an object that uses propDefault for its default values. In both cases, the property
list is empty.
Properties also contains one deprecated method: save(). This was replaced
by store() because save()did not handle errors correctly. 
 

One useful capability of the Properties class is that you can specify a default property that will
be returned if no value is associated with a certain key. For example, a default value can be
specified along with the key in the getProperty() method-such
as getProperty("name", "default value"). If the "name" value is not found, then "default value"
is returned. When you construct a Properties object, you can pass another instance
ofProperties to be used as the default properties for the new instance. In this case, if you
callgetProperty("foo") on a given Properties object, and "foo" does not exist, Java looks for
"foo" in the defaultProperties object. This allows for arbitrary nesting of levels of default
properties.
The following example demonstrates Properties. It creates a property list in which the
keys are the names of states and the values are the names of their capitals. Notice that the
attempt to find the capital for Florida includes a default value.
// Demonstrate a Property list. 
import java.util.*; 
class PropDemo { 
public static void main(String args[]) { 
Properties capitals = new Properties(); 
Set states; 
String str; 
capitals.put("Illinois", "Springfield"); 
capitals.put("Missouri", "Jefferson City"); 
capitals.put("Washington", "Olympia"); 
capitals.put("California", "Sacramento"); 
capitals.put("Indiana", "Indianapolis"); 
// Show all states and capitals in hashtable. 
states = capitals.keySet(); // get set-view of keys 
Iterator itr = states.iterator(); 
while(itr.hasNext()) { 
str = (String) itr.next(); 
System.out.println("The capital of " + 
str + " is " + 
capitals.getProperty(str) 
+ "."); 

System.out.println(); 
// look for state not in list — specify default 
str = capitals.getProperty("Florida", "Not Found"); 
System.out.println("The capital of Florida is " 
+ str + "."); 

}
The output from this program is shown here:

The capital of California is Sacramento. 


The capital of Washington is Olympia. 
The capital of Missouri is Jefferson City. 
The capital of Indiana is Indianapolis. 
The capital of Illinois is Springfield. 
The capital of Florida is Not Found. 
Since Florida is not in the list, the default value is used.
Although it is perfectly valid to use a default value when you call getProperty( ), as the
preceding example shows, there is a better way of handling default values for most
applications of property lists. For greater flexibility, specify a default property list when
constructing a Properties object. The default list will be searched if the desired key is not
found in the main list. For example, the following is a slightly reworked version of the
preceding program, with a default list of states specified. Now, when Florida is sought, it
will be found in the default list:
// Use a default property list. 
import java.util.*; 
class PropDemoDef { 
public static void main(String args[]) { 
Properties defList = new Properties(); 
defList.put("Florida", "Tallahassee"); 
defList.put("Wisconsin", "Madison"); 
Properties capitals = new Properties(defList); 
Set states; 
String str; 
capitals.put("Illinois", "Springfield"); 
capitals.put("Missouri", "Jefferson City"); 
capitals.put("Washington", "Olympia"); 
capitals.put("California", "Sacramento"); 
capitals.put("Indiana", "Indianapolis"); 
// Show all states and capitals in hashtable. 
states = capitals.keySet(); // get set-view of keys 
Iterator itr = states.iterator(); 
while(itr.hasNext()) { 
str = (String) itr.next(); 
System.out.println("The capital of " + 
str + " is " + 
capitals.getProperty(str) 
+ "."); 

System.out.println(); 
// Florida will now be found in the default list. 
str = capitals.getProperty("Florida"); 
System.out.println("The capital of Florida is " 
+ str + "."); 

}

File Navigation and I/O:


There are several other classes that we would be going through to get to know the basics of File
Navigation and I/O.

 File Class
 FileReader Class
 FileWriter Class

Directories in Java:
Creating Directories:
There are two useful File utility methods which can be used to create directories:
 The mkdir( ) method creates a directory, returning true on success and false on failure. Failure
indicates that the path specified in the File object already exists, or that the directory cannot be created
because the entire path does not exist yet.
 The mkdirs() method creates both a directory and all the parents of the directory.

Following example creates "/tmp/user/java/bin" directory:

import java.io.File;

public class CreateDir {


public static void main(String args[]) {
String dirname = "/tmp/user/java/bin";
File d = new File(dirname);
// Create directory now.
d.mkdirs();
}
}

Compile and execute above code to create "/tmp/user/java/bin".

Note: Java automatically takes care of path separators on UNIX and Windows as per conventions. If you
use a forward slash (/) on a Windows version of Java, the path will still resolve correctly.

Reading Directories:
A directory is a File that contains a list of other files and directories. When you create a File object and it
is a directory, the isDirectory( ) method will return true.

You can call list( ) on that object to extract the list of other files and directories inside. The program shown
here illustrates how to use list( ) to examine the contents of a directory:

import java.io.File;
public class DirList {
public static void main(String args[]) {
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory()) {
System.out.println( "Directory of " + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++) {
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory()) {
System.out.println(s[i] + " is a directory");
} else {
System.out.println(s[i] + " is a file");
}
}
} else {
System.out.println(dirname + " is not a directory");
}
}
}

This would produce following result:

Directory of /mysql
bin is a directory
lib is a directory
demo is a directory
test.txt is a file
README is a file
index.html is a file
include is a directory

Reading a file using FileInputStream


Java has Two types of streams- Byte & Characters. For reading and writing binary data, byte
stream is incorporated. The InputStream abstract class is used to read data from a file. The
FileInputStream is the subclass of the InputStream abstract class. The FileInputStream  is used
to read data from a file.

The read() method of an InputStream returns an int which contains the byte value of the byte
read. If there is no more data left in stream to read, the read() method returns -1 and it can be
closed after this. One thing to note here, the value -1 is an int not a byte value.

Given below example will give you a clear idea :


Example :

The content in the Devfile.txt :

The text shown here will write to a file after run

FileInStreamDemo.java

import java.io.*;

public class FileInStreamDemo {

public static void main(String[] args) {

// create file object


File file = new File("DevFile.txt");

int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;

try {
fin = new FileInputStream(file);

while ((ch = fin.read()) != -1)


strContent.append((char) ch);

fin.close();

} catch (FileNotFoundException e) {
System.out.println("File " + file.getAbsolutePath()
+ " could not be found on filesystem");
} catch (IOException ioe) {
System.out.println("Exception while reading the file" + ioe);
}

System.out.println("File contents :");


System.out.println(strContent);

}
}

Output :

The command prompt will show you the following data :

File contents :     


The text shown here will write to a file after run 

Example

Read line by only using BufferedReader

import java.io.*;

public class BufferedReaderDemo {


public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(
new FileReader("DevFile.txt"));
String devstr;
while ((devstr = br.readLine()) != null) {
System.out.println(devstr);
}
} catch (IOException e) {
}
}
}

This will produce the following Output :

Welcome To Devmanuals
Here you find the good learning stuff.
As well complete coverage on each topics like java, PHP etc.

Read line by using FileInputStream, DataInputStream, BufferedReader

import java.io.*;

public class BufferedReaderStream{


public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("DevFile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}

This will produce the following output :

Welcome To Devmanuals
Here you find the good learning stuff.
As well complete coverage on each topics like java, PHP etc.

You might also like