开发者

how to read a text file stored in mobile using j2me midp2.0?

HI ALL,

I am working in j2me midp2.0 enviro开发者_如何学JAVAnment.My application wants to read a text file which is stored in mobile device.How to read the text file programatically using j2me.Please give me idea to get this.What is the root folder in mobile to place the text file for accessible from j2me Application environment.

Saravanan.P


You need javax.microedition.io.file.FileConnection

Get root folder:

  try {
        Enumeration roots = FileSystemRegistry.listRoots();
        while(roots.hasMoreElements()) {
            System.out.println("Root: file:///"+(String)roots.nextElement());
        }
    } catch(Exception e) {
    }

write to file

        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) { }
        }
}

read from file

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) { }
    }
    }


Better u use FileConnection.

FileConnection fc=(FileConnection)Connector.ope(url);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜