Java Reference
In-Depth Information
L ISTING 11.3 Continued
11
JNLPRandomAccessFile raf = file.getRandomAccessFile(“rw”);
raf.seek(raf.length() - 1);
raf.writeUTF(“Last In file.”);
raf.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private long enlargeFile(FileContents file, int extraLength){
long length = 0L;
if (file!=null)
try {
length = file.getLength();
if (length + extraLength > file.getMaxLength()) {
length = file.setMaxLength(length + 1024);
}
} catch (Exception e) {
e.printStackTrace();
}
return length;
}
public static void main(String[] args) {
FileContentsExample fileContentsExample1 = new FileContentsExample();
}
}
Let's discuss some aspects of this code.
• The constructor (lines 12-15) calls some example methods that manipulate files on the
local file system. Note that if your application is trusted (as described in Chapter 10,
“Defining the Client Environment”), you do not need to pass through the JNLP services
to access local files.
• The method testRead tries to open up a File dialog box for accessing a file on local
disks. The method process (lines 93-99) does some simple processing on the opened
file, by copying it four bytes at a time onto the standard output. The method readLines
reads it as a text file, line by line.
• The method testWrite at lines 34-53—after having obtained a reference on a local file
and having asked to save its own file on the file system—invokes the writeData method,
beginning at line 105. This method, after checking the permissions on the given file,
treats it as a data file, writing some numerical data into it.
Search WWH ::




Custom Search