首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

j2me 当地文件访问遍历

2012-09-21 
j2me 本地文件访问遍历???????e currDir.list()???????browser new List(currDirName, List.IMPLICIT

j2me 本地文件访问遍历
??????? e = currDir.list();
??????? browser = new List(currDirName, List.IMPLICIT);
??????? browser.append(UP_DIRECTORY,null);
????
????? }
????? while (e.hasMoreElements())
????? {
??????? String fileName = (String)e.nextElement();
??????? if (fileName.charAt(fileName.length()-1) == SEP)
??????? {
????????? browser.append(fileName,null);
??????? }
??????? else
??????? {
????????? browser.append(fileName,null);
??????? }
????? }
????? 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) {}
}
}

热点排行