This article will introduce J2ME's optional package FileConnection Optional Package (hereinafter referred to as FCOP), in fact, FCOP is released in JSR75 and Personal Information Management (PIM). This article will briefly introduce the basic concept of FCOP. If the reader still doesn't know the concept of J2ME optional packages, please refer to the concept of the J2ME optional package; if the reader wants to know the details of the PIM, please refer to the J2ME Optional Package -PIM.
We know that you can access files in the JAR package in J2ME, such as reading the text files. But we can't create new files, providing the Record Management System (RMS) subsystem in MIDP to implement local persistence storage. The reason why no file operation is provided, mainly because the mobile information devices that support the file system are very small, and some mobile phones are also supported on the expansion card, not the main memory. FCOP is to solve this problem, FCOP is based on Connected Limited Devices Configuration (CLDC), so it can be seamlessly ported to the Connected Devices Configuration (CDC) compatible device.
The use of FCOP operations is mainly through two ways:
1. The first way is to access the file system for the file system via the Generic Connection Framework (GCF), which you can access files by using file: // format in the URL, such as ... import javax.microEdition. IO. *; String Url = "file: ///data.txt"; inputconnection conn = null; int mode = connection = (InputConnection) Connector.Open (URL, MODE);} catch (IOException IOE) {// no file} ...
In this case, it is also possible if your selected device does not support FCOP. CDC-compatible devices can choose to access file system files in such a way. Write files can be written by developing MODE to CONNECTOR.RIT_WRITE and CONNECTOR.WRITE_WRITE and CONNECTOR.WRITE_ONLY.
2. The second method is to perform file read and write operations by using FileConnection, first you must check if the selected device supports FileConnection. Through the following: ... // check what the file connection optional package is there
String v = system.getProperty ("microedition.io.file.fileconnection.Version");
IF (v! = null) {// fcop available} else {// fcop not available} ...
If V returns to null, the FCOP is not supported, otherwise the version number should be returned, such as 1.0.
When using FileConnection, it is also an access method of GCF. At this time, you should still use file: // as a prefix of the URL, return the type of FileConnection, the FileConnection interface extends StreamConnection, and adds a lot of ways to operate on files. These methods are very similar to java.io.file. Usually your URL should be similar to File: ///sdcard/Users.txt
Support for FCOP in WTK 2.2, you can use WTK22 to develop, pay attention to the file system when you use FCOP, you should first mount the file system. An article will detail how to use FCOP and WTK22 support for FCOP.
The following code demonstrates how to list the files in the directory:
... string url = "file: /// sdcard"; fileconnection conn = null; try {conn = (fileconnection) Connector.Open (URL); if (Conn.Indirectory ()) {ENUMERATION NAMES = conn.list ); While (names.hasmorelements ()) {string name = (string) E.NEXTELEMENT (); // do something}} else {// not a directory!}} Catch (ooException e) {// Could Not Access The url} catch (securityException e) {// no permission to read the directory} ...
If the file does not exist, you can also create a file. code show as below:
... string url = "file: ///sdcard/myfile.txt"; fileconnection conn = NULL;
Try {conn = (fileconnection) Connector.Open (URL, Connector.Write_only); if (conn.create ()) {// create the file outstream out = conn.OpenoutputStream (); // Now write data to the file}
Conn.close ();} catch (ioException e) {// error} catch (securityException e) {// no permission to create / write} ...
In java.microedition.IO.File, a FileSystemRegistry class is provided for querying the file system available, and of course there are some other roles, which will be described in detail below. We should pay attention to the method in the FileConnection may throw SECURITYEXCEPTION. However, FCOP does not define related security models, but allowing this task to configure and profile. For example, the security model defined in MIDP2.0.