1 ) Determine whether the mobile phone supports the file system
/**
* Check whether the mobile phone supports the File System
* @ Return
*/
Public Boolean issupportfilesystem (){
String v = System
. Getproperty ("microedition. Io. file. fileconnection. Version ");
If (V! = NULL) return true;
Return false;
}
Through judgmentSystemOfPropertyDetermine whether or not to exist.
(2 ) Open the file connection
Whether you want to read a file or write a file, you must first obtain Connection .
Use Connector. Open Methods, including 2 Parameters. The first parameter is the path, and the second parameter is the permission.
You have no permissions. 3 Possible reasons: Connector. Read ,Connector. Write , Connector. read_write .
Now, the path is URL Written in the form "File ://" If it is a local file, add Localhost, That is, if E:/a. dat The file path should be "File: // localhost/E:/a. dat" .
By the way, the result of the mobile phone directory is as follows: S60 The 3 So we will introduce its directory structure. Kjava Program Accessible drive 2 Items: C And E (If there is an expansion card ). E All directory files under the drive can be accessed. The path is E :/ Start;C The driver is not. Only images, sounds, and images can be accessed. Securityexception The paths of these three directories are C:/data/Images , C:/data/sounds , C:/data/videos . How do I know which directories can be accessed? Wtk Built-in Pdademo Put it on your phone and run it. Filebrowser Just test it.
fileconnection fc = (fileconnection)
connector. open (File: // localhost/E:/. dat, connector_read_write);
( 3 ) read the file
inputstream FCM = FC. openinputstream ();
it is very simple to use openinputstream open a stream, which is the same as reading network data.
( 4 ) write files
Byte [] B = "Hello world". getbytes ();
Outputstream Fos = FC. openoutputstream ();
FOS. Write (B, 0, B. Length );
The usage is the same as writing network data.OutputstreamBut pay attention to the following:N93The test results on other machines are not clear), write files are always written from the beginning, and the original content is not cleared. For example, the file contains contentAbcdefghijk,Open the file and write it into it.123What is the content of the final file? NoAbcdefghijk123Or123,123 defghijk. What should I do if I need to continue writing files? In my practice, I first read the original content toStringbuffer.StringbufferAnd then write the file. I don't know if there are any more convenient methods.
During this time, because local files are used for developmentJsr75,Jsr75It consists of two parts:File, That is, the local file system, part of which isPimTo put it bluntly, it is the phone book and other information. What I learned first isFilePart, throughImport javax. microedition. Io. file .*To allow free access to the local file system.WINDOSThe resource manager in is the same. Here I will introduce some of my writing methods.
I.Obtains the Directory and file list of a specified path.
/* Directory file list */
Public vector list (string path)
{< br> try
{< br> fileconnection fc = (fileconnection) (connector. open (PATH);
If (FC. exists ()
{< br> vector listvec = new vector (0, 1);
enumeration en = FC. list ();
while (en. hasmoreelements ()
{< br> listvec. addelement (string) (en. nextelement ();
}< br> return listvec;
}< br> else
return NULL;
}< br> catch (exception e)
{< br> system. out. println ("listerr:" + E. tostring ();
return NULL;
}< BR >}
MethodPathThe parameter is the path to be searched, suchFile: // C:/pictures/Remember, if it is a directoryPath, Then the following/It must not be saved; otherwise, content cannot be found. This method returnsPathAll directory names and file names under.
II.Create or save an object to a specified path
/* Save the file */
Public void SaveFile (string path, byte [] filedata)
{< br> try
{< br> fileconnection fc = (fileconnection) (connector. open (PATH);
FC. create ();
FC. setwritable (true);
outputstream OS = FC. openoutputstream ();
OS. write (filedata);
OS. close ();
}< br> catch (exception e)
{< br> system. out. println ("savefileerr:" + E. tostring ();
}< BR >}
FiledataIt is the content of the file to be saved. It can be a sound, an image, or a text.
Http://www.j2mehome.com/j2me/jichu/10448.html