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; iReading 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) {
}
(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
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) {
}
Comments
Post a Comment