【发布时间】:2011-08-18 01:00:19
【问题描述】:
大家好,
我在 j2me midp2.0 环境中工作。我的应用程序想要读取存储在移动设备中的文本文件。如何使用 j2me 以编程方式读取文本文件。请给我一个想法来获得这个。什么是根移动设备中的文件夹以放置文本文件,以便从 j2me 应用程序环境访问。
萨拉瓦南.P
【问题讨论】:
大家好,
我在 j2me midp2.0 环境中工作。我的应用程序想要读取存储在移动设备中的文本文件。如何使用 j2me 以编程方式读取文本文件。请给我一个想法来获得这个。什么是根移动设备中的文件夹以放置文本文件,以便从 j2me 应用程序环境访问。
萨拉瓦南.P
【问题讨论】:
你需要 javax.microedition.io.file.FileConnection
获取根文件夹:
try {
Enumeration roots = FileSystemRegistry.listRoots();
while(roots.hasMoreElements()) {
System.out.println("Root: file:///"+(String)roots.nextElement());
}
} catch(Exception e) {
}
写入文件
public void write(String root) {
FileConnection fc = null;
String fName = "test.txt";
try {
fc = (FileConnection) Connector.open(root + fName, Connector.READ_WRITE);
if(!fc.exists()) {
fc.create();
}
DataOutputStream dos = fc.openDataOutputStream();
dos.writeUTF("test-test");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fc.close();
} catch (IOException e) { }
}
}
从文件中读取
public void read(String root) {
FileConnection fc = null;
try {
fc= (FileConnection) Connector.open(root + "test.txt", Connector.READ);
DataInputStream dis = fc.openDataInputStream();
String data = dis.readUTF();
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fc.close();
} catch (IOException e) { }
}
}
【讨论】:
最好使用 FileConnection。
FileConnection fc=(FileConnection)Connector.ope(url);
【讨论】: