Professional Documents
Culture Documents
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.
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);
}
}
Properties defaults;
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:
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.
import java.io.File;
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");
}
}
}
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
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.
FileInStreamDemo.java
import java.io.*;
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
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);
}
}
}
Output :
Example
import java.io.*;
Welcome To Devmanuals
Here you find the good learning stuff.
As well complete coverage on each topics like java, PHP etc.
import java.io.*;
Welcome To Devmanuals
Here you find the good learning stuff.
As well complete coverage on each topics like java, PHP etc.