2010-12-21 29 views

回答

5

你需要javax.microedition.io.file.FileConnection

獲得root文件夾:

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

嗨oxigen,在這裏,我提到的文本文件名?請指導我 – Saravanan 2010-12-21 10:37:05

+0

小樣本加入 – oxigen 2010-12-21 12:38:45

+0

謝謝,我會檢查它,讓你知道我的答案...非常感謝... – Saravanan 2010-12-21 12:58:33

1

同相關主題的諾基亞論壇討論。供您參考看到這個link...這可能是幫助你... :)

1

更好u使用的FileConnection。

FileConnection fc=(FileConnection)Connector.ope(url); 
相關問題