您的位置:首页 > Web前端 > JavaScript

J2ME文件系统的操作(JSR75)

2010-01-06 22:24 351 查看
最近由于工作的需要,研究了一下J2ME的文件系统操作(JSR75),对开发经历介绍一下。

(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 command null cmd file