J2ME文件系统的操作(JSR75)
2010-01-06 22:24
351 查看
最近由于工作的需要,研究了一下J2ME的文件系统操作(JSR75),对开发经历介绍一下。
(1)首先判断手机是否支持J2ME的JSR75,若手机不支持JSR75下面的工作将是无意义的。判断手机是否支持JSR75,若支持并查询其根目录路径,其代码清单如下。若支持,则返回一个版本信息1.0;若不支持,则返回一个负数。
(2)建立文件系统的连接关联。
无论是要读文件、写文件或删除文件,首先必须得到该文件的Connection。可使用Connector.open方法,有2个参数,第一个参数是路径,第二个参数是权限。 权限没什么好说的了,一共就3个可能:Connector.READ、Connector.WRITE、Connector.READ_WRITE。现在来说说路径,路径是以URL形式书写的,以"file://"开头,如果是本机上的文件就加上localhost,即如果是E:/1.txt,文件的路径应该是"file://localhost/E:/1.txt”。通常模拟器的路径为"file://localhost/root1/1.txt”,在笔者的实验过程中使用的是MTK系统,其记录存储的文件路径是"file://localhost/PhoneDisk:/1.txt”,TF卡的文件路径"file://localhost/MemoryCard:/1.txt”。读者可以根据自己检测得具体手机的根目录路径,来配置文件的关联路径。
(3)文件的读写操作
文件的读写操作,就是打开相应的输入、输出流和读取一般数据流一致。
读取数据 首先,InputStream is = fc.openInputStream();接着 is.read() ,最后将read()出来的数据放置到一个空间就OK了。
写入数据 首先,OutputStream os = fc.openOutputStream() ;接着os.write(byte)。
代码的优化,及功能的添加在以后讨论!
(1)首先判断手机是否支持J2ME的JSR75,若手机不支持JSR75下面的工作将是无意义的。判断手机是否支持JSR75,若支持并查询其根目录路径,其代码清单如下。若支持,则返回一个版本信息1.0;若不支持,则返回一个负数。
package fuhuaking.filesystem; import java.util.Enumeration; import javax.microedition.io.file.FileSystemRegistry; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * @function 判断手机是否支持JSR75,并打印出根目录路径 * @author li.fuwa * 2010-1-6 */ public class CheckMidlet extends MIDlet { private Display display; private Form form ; private String version ; private Enumeration en; public CheckMidlet() { display = Display.getDisplay(this); form = new Form("version"); version = System.getProperty("microedition.io.file.FileConnection.version"); form.append(version);//送至窗体显示 //送至控制面板显示 if(version == null) { System.out.println("cannot control fileSystem!"); return; }else{ System.out.println("version: " + version); } display.setCurrent(form); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp() {} protected void startApp() throws MIDletStateChangeException { en = FileSystemRegistry.listRoots(); String tmp = ""; while (en.hasMoreElements()) { tmp = en.nextElement().toString(); //送至窗体 form.append("root is"); form.append(tmp); System.out.println("root: " + tmp);//送至控制面板 } } }
(2)建立文件系统的连接关联。
无论是要读文件、写文件或删除文件,首先必须得到该文件的Connection。可使用Connector.open方法,有2个参数,第一个参数是路径,第二个参数是权限。 权限没什么好说的了,一共就3个可能:Connector.READ、Connector.WRITE、Connector.READ_WRITE。现在来说说路径,路径是以URL形式书写的,以"file://"开头,如果是本机上的文件就加上localhost,即如果是E:/1.txt,文件的路径应该是"file://localhost/E:/1.txt”。通常模拟器的路径为"file://localhost/root1/1.txt”,在笔者的实验过程中使用的是MTK系统,其记录存储的文件路径是"file://localhost/PhoneDisk:/1.txt”,TF卡的文件路径"file://localhost/MemoryCard:/1.txt”。读者可以根据自己检测得具体手机的根目录路径,来配置文件的关联路径。
(3)文件的读写操作
文件的读写操作,就是打开相应的输入、输出流和读取一般数据流一致。
读取数据 首先,InputStream is = fc.openInputStream();接着 is.read() ,最后将read()出来的数据放置到一个空间就OK了。
写入数据 首先,OutputStream os = fc.openOutputStream() ;接着os.write(byte)。
package fuhuaking.filesystem; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * @function J2ME JSR75文件的写入、读取和删除 * @author li.fuwa * 2010-1-6 */ public class FileSystem extends MIDlet implements CommandListener{ //private Display display; private Form form; private String filePath = "file://localhost/root1/1.txt";//模拟器路径 private TextField tf = new TextField("Write Data", "test", 100, TextField.ANY); private Command exit_cmd = new Command("Exit", Command.EXIT, 1); private Command write_cmd = new Command("write", Command.OK, 1); private Command read_cmd = new Command("read", Command.OK, 1); private Command delect_cmd = new Command("dele", Command.OK, 1); private FileConnection fc = null; private OutputStream os = null; private InputStream is = null; public FileSystem() { form = new Form("FileSystem Demo"); form.append(tf); form.addCommand(write_cmd); form.addCommand(read_cmd); form.addCommand(delect_cmd); form.addCommand(exit_cmd); form.setCommandListener(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp() {} protected void startApp() throws MIDletStateChangeException { Display.getDisplay(this).setCurrent(form); } /** * 写文件 * @param b */ private void write( byte[] b) { try { fc = (FileConnection) Connector.open(filePath); if (!fc.exists())// 若文件不存在 fc.create();// 创建文件 else fc.truncate(0);// 清空文件数据 os = fc.openOutputStream(); os.write(b);// 写入文件数据 os.close(); os = null; form.append("wirte finish"); fc.close(); fc = null; } catch (IOException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } os = null; } if (fc != null) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } fc = null; } } } /** * 读文件 */ private void read() { try { fc = (FileConnection) Connector.open(filePath); if (!fc.exists()) {// 若文件不存在 return; } is = fc.openInputStream(); byte[] b = new byte[(int) fc.fileSize()]; is.read(b);// 读出文件数据 form.append("Read Finish. content is"); form.append(new String(b)); is.close(); is = null; fc.close(); fc = null; } catch (IOException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } if (fc != null) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } fc = null; } } } /** * 删除文件 * */ private void delect() { try { fc = (FileConnection) Connector.open(filePath); if (!fc.exists()) { form.append("file is not exists"); return; } else if (fc.exists()) { fc.delete(); form.append("dele finish. dele filePath is"); form.append(fc.getURL()); fc.close(); fc = null; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fc != null) { fc.close(); } } catch (IOException e) { e.printStackTrace(); } fc = null; } } public void commandAction(Command c, Displayable arg1) { if (c == write_cmd) {// 读文件 new Thread() { public void run() { write(tf.getString().getBytes()); } }.start(); //form.removeCommand(write_cmd); //form.addCommand(read_cmd); } else if (c == read_cmd) {// 写文件 new Thread() { public void run() { read(); } }.start(); // form.removeCommand(read_cmd); //form.addCommand(write_cmd); } else if(c == delect_cmd){//删除文件 new Thread() { public void run() { delect(); } }.start(); } else if (c == exit_cmd) {// 退出 try { destroyApp(true); } catch (MIDletStateChangeException e) { e.printStackTrace(); } notifyDestroyed(); } } }
代码的优化,及功能的添加在以后讨论!
相关文章推荐
- j2me中的文件操作包JSR75入门讲解
- J2ME程序操作文件系统
- J2ME的文件系统操作
- [J2ME]文件操作-JSR75包使用总结
- J2ME程序操作文件系统
- j2me程序操作文件系统
- jsr75 fileconnection 文件操作的方法.
- J2ME程序操作文件系统
- J2ME程序操作文件系统
- J2ME程序操作文件系统
- Node.js文件系统、路径的操作函数
- UNIX系统 文件操作接口
- 实验楼Linux学习笔记(六)之文件系统操作与磁盘管理
- OC_文件系统操作
- 文件系统操作与磁盘管理
- 2011年11月28日——进程管理,内存管理,文件系统,系统管理,网络操作概述
- 文件系统操作与磁盘管理
- delphi 监控系统文件操作
- Linux基本操作 2-----Linux文件系统基本结构
- C# C/S 结构操作Ini系统文件