【问题标题】:how to read a text file stored in mobile using j2me midp2.0?如何使用 j2me midp2.0 读取存储在手机中的文本文件?
【发布时间】:2011-08-18 01:00:19
【问题描述】:

大家好,

我在 j2me midp2.0 环境中工作。我的应用程序想要读取存储在移动设备中的文本文件。如何使用 j2me 以编程方式读取文本文件。请给我一个想法来获得这个。什么是根移动设备中的文件夹以放置文本文件,以便从 j2me 应用程序环境访问。

萨拉瓦南.P

【问题讨论】:

    标签: java-me mobile


    【解决方案1】:

    你需要 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) { }
        }
        }
    

    【讨论】:

    • 嗨 oxigen,在这里,我在哪里提到了文本文件名?请指导我
    • 我在 netbeans 中运行我的应用程序。但它在运行时引发错误 Root_dir is:file:///root1/ [ERRR] [fileconnection] Error: javacall_fileconnection_is_directory(), cannot get file attributes [ERRR] [fileconnection] Error: javacall_fileconnection_dir_exists(), cannot access directory java. io.IOException: 目标文件不存在
    • 对不起,这是我的问题..我解决了它的工作正常...非常感谢
    【解决方案2】:

    最好使用 FileConnection。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多