home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 1.3 KB | 55 lines |
- import java.io.*;
-
- class Ran {
- static String fileName = "Ran.test";
-
- public static void main(String[] args) {
- try {
- sayHello();
- appendHi();
- readGreeting();
- } catch (IOException x) {
- System.out.println(x.getMessage());
- }
- }
-
- public static void sayHello() throws IOException {
- DataOutputStream ds = null;
- try {
- File f = new File(fileName);
- FileOutputStream out = new FileOutputStream(f);
- ds = new DataOutputStream(out);
- ds.writeBytes("hello!");
- } finally {
- if (ds != null)
- ds.close();
- }
- }
-
- public static void appendHi() throws IOException {
- RandomAccessFile out = null;
- try {
- File f = new File(fileName);
- out = new RandomAccessFile(f, "rw");
- out.seek(out.length());
- out.writeBytes(" hi!");
- } finally {
- if (out != null)
- out.close();
- }
- }
-
- public static void readGreeting() throws IOException {
- RandomAccessFile in = null;
- try {
- File f = new File(fileName);
- in = new RandomAccessFile(f, "r");
- String s = in.readLine();
- System.out.println(s);
- } finally {
- if (in != null)
- in.close();
- }
- }
- }
-