Code below demonstrates how read and write a file using an applet.
int arrlen = 10000;
byte[] infile = new byte[arrlen];
Frame parent = new Frame();
FileDialog fd = new FileDialog(parent, "Please choose a file:",
FileDialog.LOAD);
fd.show();
String selectedItem = fd.getFile();
if (selectedItem == null) {
// no file selected
} else {
File ffile = new File( fd.getDirectory() + File.separator +
fd.getFile());
// read the file
System.out.println("reading file " + fd.getDirectory() +
File.separator + fd.getFile() );
try {
FileInputStream fis = new FileInputStream(ffile);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try {
int filelength = dis.read(infile);
String filestring = new String(infile, 0, filelength);
System.out.println("FILE CONTENT=" + filestring);
} catch(IOException iox) {
System.out.println("File read error...");
iox.printStackTrace();
}
} catch (FileNotFoundException fnf) {
System.out.println("File not found...");
fnf.printStackTrace();
}
}