转发:J2ME FileConnection开发

下面是对开发J2ME FileConnection的一些总结:
1、J2ME FileConnection在WTK能正常运行,但拿到S60可能会报错,因他们之间存在差异。


3、在WTK能用fc.exists()来判断文件是否存在,但在S60中会抛出异常,创建文件如下:



Java代码
private void createFile(String fileName) {
String fileExt = "";
{
int idx = fileName.lastIndexOf('.');
if (idx > 0) {
fileExt = fileName.substring(idx, fileName.length());
fileName = fileName.substring(0, idx);
}
}
try {
currDir = (FileConnection) Connector.open("file:///" + currDirName
+ fileName + fileExt, Connector.WRITE);
currDir.create();
}catch (SecurityException e) {
Alert alert = new Alert("Error!", "Can not access/delete file "
+ fileName + " in directory " + currDirName
+ "\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
FileMIDlet.display.setCurrent(alert);
}catch (IOException e) {
e.printStackTrace();
try {
currDir = (FileConnection) Connector.open("file:///"
+ currDirName + fileName + "(" + genId() + ")"
+ fileExt, Connector.WRITE);
currDir.create();
} catch (IOException e1) {
// TODO Auto-generated catch block
currDir = null;
}
}
}

private void createFile(String fileName) {
String fileExt = "";
{
int idx = fileName.lastIndexOf('.');
if (idx > 0) {
fileExt = fileName.substring(idx, fileName.length());
fileName = fileName.substring(0, idx);
}
}
try {
currDir = (FileConnection) Connector.open("file:///" + currDirName
+ fileName + fileExt, Connector.WRITE);
currDir.create();
}catch (SecurityException e) {
Alert alert = new Alert("Error!", "Can not access/delete file "
+ fileName + " in directory " + currDirName
+ "\nException: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
FileMIDlet.display.setCurrent(alert);
}catch (IOException e) {
e.printStackTrace();
try {
currDir = (FileConnection) Connector.open("file:///"
+ currDirName + fileName + "(" + genId() + ")"
+ fileExt, Connector.WRITE);
currDir.create();
} catch (IOException e1) {
// TODO Auto-generated catch block
currDir = null;
}
}
}



4、S60无法浏览JAD,JAD文件,保存JAD或JAR时也会抛出“writing to the file is not allowed”异常,解决办法是加个后缀名,如:yongmail.jad.重命名
5、有些手机不支持文件系统,所以最好用接口方式

6、 解决java.lang.SecurityException: Access denied,可用上面的例子!

7、测试文件系统时,最好用真机





导读
手机中的重定向问题及处理 http://fonter.iteye.com/blog/400836
J2ME如何通过cmwap直接访问互连网 http://fonter.iteye.com/blog/400868
J2ME开发中由WAP网关带来的cookie问题 http://fonter.iteye.com/blog/399105
安装JAD时提示JAD无效原因之一 http://fonter.iteye.com/blog/400888
J2ME网络交互之优化 http://fonter.iteye.com/blog/405137
解决Eclipse无法调试J2ME程序的配置方法 http://fonter.iteye.com/blog/405697

J2ME飞信协议分析(初稿) http://fonter.iteye.com/blog/408385

转发:http://fonter.iteye.com/blog/441946
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值