6.6. File Access¶
6.6.1. Dependencies¶
File access requires the FS foundation library.
To use it, add the following dependency to your project’s module.ivy file:
<dependency org="ej.api" name="fs" rev="2.0.6" />
6.6.2. Replacing the javax.microedition.io.file.FileConnection class¶
Code using javax.microedition.io.file.FileConnection can use java.io.File, java.io.FileInputStream and java.io.FileOutputStream instead.
The following snippet is extracted from the JSR-75 Javadoc.
try {
FileConnection fconn = (FileConnection)Connector.open("file:///CFCard/newfile.txt");
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists())
fconn.create(); // create the file if it doesn't exist
fconn.close();
}
catch (IOException ioe) {
}
It can be adapted as follows:
try {
File fconn = new File("/CFCard/newfile.txt");
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists()) {
fconn.createNewFile(); // create the file if it doesn't exist
}
} catch (IOException ioe) {
}
Moreover, calls to openInputStream(), openDataInputStream(), openOutputStream() and openDataOutputStream() can be replaced as follows:
File file = new File(fileName);
// FileInputStream
FileInputStream fis = new FileInputStream(file); // or new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fis);
// FileOutputStream
FileOutputStream fos = new FileOutputStream(file); // or new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(fos);