---
title: "J2ME FileConnection开发"
author: "fonter"
date: "2009-08-07T00:00:39Z"
tags: []
slug: "j2me-fileconnection"
draft: false
lastmod: "2009-08-13T12:03:50Z"
id: 441946
url: "https://fonter.iteye.com/blog/441946"
---

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

![](http://www.iteye.com/upload/attachment/133151/64892d65-608b-3758-b199-03c2f596f57f.jpg)   
3、在WTK能用fc.exists()来判断文件是否存在，但在S60中会抛出异常，创建文件如下：

 

```
	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](/blog/400836%20)  
J2ME如何通过cmwap直接访问互连网 [http://fonter.iteye.com/blog/400868](/blog/400868)  
J2ME开发中由WAP网关带来的cookie问题 [http://fonter.iteye.com/blog/399105](/blog/399105)  
安装JAD时提示JAD无效原因之一 [http://fonter.iteye.com/blog/400888](/blog/400888)  
J2ME网络交互之优化 [http://fonter.iteye.com/blog/405137](/blog/405137%20)  
解决Eclipse无法调试J2ME程序的配置方法 [http://fonter.iteye.com/blog/405697](/blog/405697)

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