分享
 
 
 

使用FileConnection访问文件系统

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

The FileConnection API specified in JSR-75 gives Access to the local file systems on devices like PDA. In order to overcome security issues MIDlet needs to include requested file permission in its JAD file under MIDLet-Permission property.The example below has a file browser to move around the file system, select and read images stored in its local image Directory.

import Java.util.*;

import java.io.*;

import javax.microedition.io.*;

import javax.microedition.io.file.*;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class FileMidlet extends MIDlet implements CommandListener

{

private String currDirName;

private Command view = new Command("View", Command.ITEM, 1);

private Command back = new Command("Back", Command.BACK, 2);

private Command exit = new Command("Exit", Command.EXIT, 3);

private final static String UP_DIRECTORY = "..";

private final static String MEGA_ROOT = "/";

private final static String SEP_STR = "/";

private final static char SEP = '/';

public FileMidlet()

{

currDirName = MEGA_ROOT;

}

public void startApp()

{

boolean isAPIAvailable = false;

if (System.getProperty(

"microedition.io.file.FileConnection.version") != null)

{

isAPIAvailable = true;

try

{

showCurrDir();

}

catch (SecurityException e)

{}

catch (Exception e) {}

}

else

{

StringBuffer splashText = new StringBuffer(

getAppProperty("MIDlet-Name")).append("\n").append(

getAppProperty("MIDlet-Vendor")).

append(isAPIAvailable?"":"\nFileConnection API not available");

Alert splashScreen = new Alert(null,splashText.toString(),

null,AlertType.INFO);

splashScreen.setTimeout(3000);

Display.getDisplay(this).setCurrent(splashScreen);

}

}

public void pauseApp() {}

public void destroyApp(boolean cond)

{

notifyDestroyed();

}

public void commandAction(Command c, Displayable d)

{

if (c == view)

{

List curr = (List)d;

final String currFile = curr.getString(curr.getSelectedIndex());

new Thread(new Runnable()

{

public void run()

{

if (currFile.endsWith(SEP_STR)

currFile.equals(UP_DIRECTORY))

{

traverseDirectory(currFile);

} else

{

showFile(currFile);

}

}

}).start();

}

else if (c == back)

{

showCurrDir();

}

else if (c == exit)

{

destroyApp(false);

}

}

void showCurrDir()

{

Enumeration e;

FileConnection currDir = null;

List browser;

try

{

if (MEGA_ROOT.equals(currDirName))

{

e = FileSystemRegistry.listRoots();

browser = new List(currDirName, List.IMPLICIT);

}

else

{

currDir = (FileConnection)Connector.open(

"file://localhost/" + currDirName);

e = currDir.list();

browser = new List(currDirName, List.IMPLICIT);

browser.append(UP_DIRECTORY);

}

while (e.hasMoreElements())

{

String fileName = (String)e.nextElement();

if (fileName.charAt(fileName.length()-1) == SEP)

{

browser.append(fileName);

}

else

{

browser.append(fileName);

}

}

browser.setSelectCommand(view);

browser.addCommand(exit);

browser.setCommandListener(this);

if (currDir != null)

{

currDir.close();

}

Display.getDisplay(this).setCurrent(browser);

}

catch (IOException ioe)

{}

}

void traverseDirectory(String fileName)

{

if (currDirName.equals(MEGA_ROOT))

{

if (fileName.equals(UP_DIRECTORY))

{

// can not go up from MEGA_ROOT

return;

}

currDirName = fileName;

}

else if (fileName.equals(UP_DIRECTORY))

{

// Go up one directory

// TODO use setFileConnection when implemented

int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);

if (i != -1)

{

currDirName = currDirName.substring(0, i+1);

}

else

{

currDirName = MEGA_ROOT;

}

}

else

{

currDirName = currDirName + fileName;

}

showCurrDir();

}

void showFile(String fileName)

{

try

{

FileConnection fc = (FileConnection)

Connector.open("file://localhost/" + currDirName + fileName);

if (!fc.exists())

{

throw new IOException("File does not exists");

}

InputStream fis = fc.openInputStream();

byte[] b = new byte[1024];

int length = fis.read(b, 0, 1024);

fis.close();

fc.close();

TextBox tb = new TextBox("View File: " + fileName, null, 1024,

TextField.ANY TextField.UNEDITABLE);

tb.addCommand(back);

tb.addCommand(exit);

tb.setCommandListener(this);

if (length > 0)

{

tb.setString(new String(b, 0, length));

}

Display.getDisplay(this).setCurrent(tb);

}

catch (Exception e) {}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有