读书人

! j2me怎么遍历手机目录

发布时间: 2012-03-01 10:25:47 作者: rapoo

求助! j2me如何遍历手机目录
j2me中用FileConnection如何遍历手机目录中的所有文件??? 希望给出代码片段,谢谢

[解决办法]
mark
[解决办法]
FileSystemRegistry.listRoots()
FileConnection.list()

[解决办法]
用楼上说的那几个接口,,遍历是可以实现的,,不难,,,不过有些手机是有存储卡的 可能要几个盘,要让用户选择
[解决办法]
用到的接口,我都知道,关键是这个遍历的思路,我不是太清晰,希望大家指点,谢谢!
[解决办法]
jsr75
[解决办法]
楼主大哥 小弟想问问怎样读取手机目录 小弟是想读取手机铃声。
[解决办法]
package example.fc;

import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
* Demonstration MIDlet for File Connection API. This MIDlet implements simple
* file browser for the filesystem avaliable to the J2ME applications.
*
*/
public class FileBrowser extends MIDlet implements CommandListener {

private String currDirName;

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

private Command write = new Command( "Write ", Command.ITEM, 2);

private Command creat = new Command( "New ", Command.ITEM, 2);

private Command creatOK = new Command( "OK ", Command.OK, 1);

private Command save = new Command( "save ", Command.OK, 1);

private Command prop = new Command( "Properties ", Command.ITEM, 2);

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

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

private String writeFileName = " ";

private TextField nameInput; // Input field for new file name

private TextField content;

private ChoiceGroup typeInput; // Input fiels for file type (regular/dir)

private final static String[] attrList = { "Read ", "Write ", "Hidden " };

private final static String[] typeList = { "Regular File ", "Directory " };

private final static String[] monthList = { "Jan ", "Feb ", "Mar ", "Apr ",
"May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec " };

private Image dirIcon, fileIcon;

private Image[] iconList;

/* special string denotes upper directory */
private final static String UP_DIRECTORY = ".. ";

/*
* special string that denotes apper directory accessible by this browser.
* this virtual directory contains all roots.
*/
private final static String MEGA_ROOT = "/ ";

/* separator string as defined by FC specification */
private final static String SEP_STR = "/ ";

/* separator character as defined by FC specification */
private final static char SEP = '/ ';

public FileBrowser() {
currDirName = MEGA_ROOT;
try {
dirIcon = Image.createImage( "/icons/dir.png ");
} catch (IOException e) {
dirIcon = null;
}
try {
fileIcon = Image.createImage( "/icons/file.png ");


} catch (IOException e) {
fileIcon = null;
}
iconList = new Image[] { fileIcon, dirIcon };
}

public void startApp() {
try {
showCurrDir();
} catch (SecurityException e) {
Alert alert = new Alert( "Error ",
"You are not authorized to access the restricted API ",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Form form = new Form( "Cannot access FileConnection ");
form
.append(new StringItem(
null,
"You cannot run this MIDlet with the current permissions. "
+ "Sign the MIDlet suite, or run it in a different security domain "));
form.addCommand(exit);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(alert, form);
} catch (Exception e) {
e.printStackTrace();
}
}

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 {
// Show file contents
showFile(currFile);
}
}
}).start();
} else if (c == write) {

List curr = (List) d;
final String currFile = curr.getString(curr.getSelectedIndex());
if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
traverseDirectory(currFile);
} else {
// Show file contents
writeFileName = currFile;
showInputFiled();
}

} else if (c == save) {
new Thread(new Runnable() {
public void run() {
String string= " ";
try {

FileConnection fc = (FileConnection) Connector
.open( "file://localhost/ " + currDirName + writeFileName);
InputStream fis = fc.openInputStream();
byte[] b = new byte[1024];

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

fis.close();
fc.close();


if (length > 0) {
string=new String(b, 0, length);
}
}catch(IOException e){

}
string+= "\n ";
string+=content.getString();
byte[] b = string.getBytes();
saveFile(writeFileName, b);
showFile(writeFileName);
}
}).start();

} else if (c == prop) {
List curr = (List) d;
String currFile = curr.getString(curr.getSelectedIndex());

showProperties(currFile);
} else if (c == creat) {
createFile();
} else if (c == creatOK) {
String newName = nameInput.getString();
if (newName == null || newName.equals( " ")) {


Alert alert = new Alert( "Error! ",
"File Name is empty. Please provide file name ", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert);
} else {
// Create file in a separate thread and disable all commands
// except for "exit "
executeCreateFile(newName, typeInput.getSelectedIndex() != 0);
Display.getDisplay(this).getCurrent().removeCommand(creatOK);
Display.getDisplay(this).getCurrent().removeCommand(back);
}

} else if (c == back) {
showCurrDir();
} else if (c == exit) {
destroyApp(false);
}
}

// Starts creatFile with another Thread
private void executeCreateFile(final String name, final boolean val) {
new Thread(new Runnable() {
public void run() {
createFile(name, val);
}
}).start();
}


[解决办法]
看看 "j2me开发精解 "

读书人网 >J2ME开发

热点推荐