2016-05-21 50 views
1

我試圖讀取此answer後面的txt文件,但我無法使其工作。它顯示了此異常:J2ME異常打開文件:根目錄不可訪問

java.io.IOException: Root is not accessible 
- com/sun/io/j2me/file/Protocol..unknown.(), bci=21 
- com/sun/io/j2me/file/Protocol..unknown.(), bci=424 
- com/sun/io/j2me/file/Protocol..unknown.(), bci=5 
- javax/microedition/io/Connector.open(), bci=73 
- javax/microedition/io/Connector.open(), bci=6 
- javax/microedition/io/Connector.open(), bci=3 

這是代碼:

try 
{      
    fileConnection = (FileConnection)Connector.open("file://home//pi//test.txt", Connector.READ_WRITE); 

    if(fileConnection.exists()) 
    { 
     int size = (int)fileConnection.fileSize(); 
     is= fileConnection.openInputStream(); 
     byte bytes[] = new byte[size]; 
     is.read(bytes, 0, size); 
     String str = new String(bytes, 0, size); 

     System.out.println(size); 
    } 

} 

我認爲它有相應的權限,但我已經設置這些:

javax.microedition.io.Connector.file.read 

javax.microedition.io.Connector.file.write 

java.io.FilePermission "file://*" "read,write" 

人幫我?

由於提前,

+0

我自己找到了解決方案,路徑不是以'home'開頭,而是以'root1' – cuoka

回答

0

有些/大多數支持的JavaME的手機不允許訪問所有文件夾在SD卡或內部存儲上。使用String file = "file:///c:/other/test.txt";String file = "file:///e:/other/test.txt";代替。

或者你可以嘗試String file = System.getProperty("fileconn.dir.memorycard") + "/test.txt";String file = System.getProperty("fileconn.dir.memorycard") + "/other/test.txt";

最好的方法但是,是使用代碼,簡單地檢查可用的文件夾。這裏有一些有用的例子: http://javatechig.com/java/j2me/fileconnection-apis-jsr-75