2013-07-05 61 views
0

現在我正在開發一個項目,我需要在SD卡中創建一個能夠完成的文件夾。此外,我需要根據需要隱藏/取消隱藏它。該代碼在仿真器上正常工作,但不在設備中,這是我的代碼出了什麼問題?錯誤:文件不存在

public class FolderCreate extends MIDlet { 
    private Form form; 
    private Display display; 
    FileConnection fc; 
    String path; 

     public void startApp() {   

     form = new Form("Hello World"); 
     String msg = "Hello World!!!!!!!"; 
     form.append(msg); 
     display = Display.getDisplay(this); 
     display.setCurrent(form); 
    System.out.println("WWWW");   
     try { 
      path = System.getProperty("fileconn.dir.memorycard"); 
      System.out.println("Path : "+path+"/sample"); 
       fc = (FileConnection)Connector.open(path+"/ABCD/"); 
       if(!fc.exists()) 
       { 
        fc.mkdir(); 
        System.out.println("directory created");     
       }    

     } catch (IOException e) { 
       // TODO Auto-generated catch block 
       //System.out.println("ERROR "+e.getMessage()); 
      Alert alert = new Alert("Alert"); 
      alert.setString(e.getMessage()); 
      display.setCurrent(alert); 
     } 
     try 
     { 
      //fc = (FileConnection)Connector.open(path+"/sample/"); 
      if(fc.isHidden()) 
       { 
        fc.setHidden(false); 
       } 
      else{ 
        fc.setHidden(true); 
      } 
     fc.close(); 
     } 
     catch (Exception e) 
     { 
      Alert alert = new Alert("Alert2"); 
      alert.setString(e.toString()); 
      display.setCurrent(alert); 
     } 




    } 

    public void pauseApp() { 
    } 

    public void destroyApp(boolean unconditional) { 
     System.out.println("Destroyed"); 
     notifyDestroyed(); 
    } 
} 

錯誤時得到的是:java.io.IOException:文件不存在

回答

-1

,我認爲你是在下面的行做的錯誤,

path = System.getProperty("fileconn.dir.memorycard"); 

當你用手機和SD卡工作您應該使用e:驅動器參照SD卡如下,

path = file:///e:/<folder-name>/ 
+0

此方法(addind「e:」)不保證能夠與所有設備一起使用。 :( –

0

檢查path是否以"file://"開頭。如果不是,請添加後綴。

path = System.getProperty("fileconn.dir.memorycard"); 
if (path != null && !path.startsWith("file://")) { 
    path = "file://" + path; 
} 
相關問題