Source code for Java Files, Streams, I/O (java.io) Handling


Constructing a Path
On Windows, this example creates the path \blash a\blash b. On Unix, the path would be /a/b.

String path = File.separator +
"a" + File.separator + "b";

Reading Text from Standard Input
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String str = "";
while (str != null) {
System.out.print("> prompt ");
str = in.readLine();
process(str);
}
} catch (IOException e) {
}

Reading Text from a File
try {
BufferedReader in = new BufferedReader(
new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}

Writing to a File
If the file does not already exist, it is automatically created.
try {
BufferedWriter out = new BufferedWriter(
new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
}

Creating a Directory
(new File("directoryName")).mkdir();

Appending to a File
try {
BufferedWriter out = new BufferedWriter(
new FileWriter("filename", true));
out.write("aString");
out.close();
} catch (IOException e) {
}
Deleting a File
(new File("filename")).delete();

Deleting a Directory
(new File("directoryName")).delete();

Creating a Temporary File
try {
// Create temp file.
File temp = File.createTempFile(
"pattern", ".suffix");

// Delete temp file when program exits.
temp.deleteOnExit();

// Write to temp file
BufferedWriter out = new BufferedWriter(
new FileWriter(temp));
out.write("aString");
out.close();
} catch (IOException e) {
}

Using a Random Access File
try {
File f = new File("filename");
RandomAccessFile raf =
new RandomAccessFile(f, "rw");

// Read a character.
char ch = raf.readChar();

// Seek to end of file.
raf.seek(f.length());

// Append to the end.
raf.writeChars("aString");
raf.close();
} catch (IOException e) {
}
Serializing an Object
The object to be serialized must implement java.io.Serializable.
try {
ObjectOutput out = new ObjectOutputStream(
new FileOutputStream("filename.ser"));
out.writeObject(object);
out.close();
} catch (IOException e) {
}

Deserializing an Object
This example deserializes a java.awt.Button object.
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("filename.ser"));
AnObject object = (AnObject) in.readObject();
in.close();
} catch (ClassNotFoundException e) {
} catch (IOException e) {
}

Traversing a Directory
public static void traverse(File f) {
process(f);

if (f.isDirectory()) {
String[] children = f.list();
for (int i=0; i traverse(new File(f, children[i]));
}
}
}

Reading UTF-8 Encoded Data
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(
"infilename"), "UTF8"));
String str = in.readLine();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}

Writing UTF-8 Encoded Data
try {
Writer out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(
"outfilename"), "UTF8"));
out.write(aString);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}

Reading ISO Latin-1 Encoded Data
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(
"infilename"), "8859_1"));
String str = in.readLine();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}

Writing ISO Latin-1 Encoded Data
try {
Writer out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(
"outfilename"), "8859_1"));
out.write(aString);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {

No comments: