2010-03-07 59 views
0

我有以下代碼:J2ME java.io.IOException的錯誤

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import java.io.*; 
import javax.microedition.io.*; 

public class FileConnection extends MIDlet implements CommandListener, Runnable { 
    private Command exit, start; 
    private Display display; 
    private Form form; 
    public FileConnection() 
    { 
    display = Display.getDisplay(this); 
    exit = new Command("Exit", Command.EXIT, 1); 
    start = new Command("Start", Command.EXIT, 1); 
    form = new Form("Write To File"); 
    form.addCommand(exit); 
    form.addCommand(start); 
    form.setCommandListener(this); 
    } 
    public void startApp() throws MIDletStateChangeException 
    { 
    display.setCurrent(form); 
    } 

    public void run(){ 
     try{ 
      javax.microedition.io.file.FileConnection filecon = 
      (javax.microedition.io.file.FileConnection) 
      Connector.open("file:///root1/photos/fisier.txt", Connector.WRITE); 
    OutputStream out = filecon.openOutputStream(); 
    PrintStream output = new PrintStream(out); 
    output.println("This is a test."); 
    out.close(); 
    filecon.close(); 
    Alert alert = new Alert("Completed", "Data Written", null, null); 
    alert.setTimeout(Alert.FOREVER); 
    alert.setType(AlertType.ERROR); 
    display.setCurrent(alert); 
     } 
     catch(ConnectionNotFoundException error) 
     { 
     Alert alert = new Alert(
      "Error", "Cannot access file.", null, null); 
     alert.setTimeout(Alert.FOREVER); 
     alert.setType(AlertType.ERROR); 
     display.setCurrent(alert);  
     } 
     catch(IOException error) 
     { 
     Alert alert = new Alert("Error", error.toString(), null, null); 
     alert.setTimeout(Alert.FOREVER); 
     alert.setType(AlertType.ERROR); 
     display.setCurrent(alert);  
     } 
    } 

    public void pauseApp() 
    { 
    } 
    public void destroyApp(boolean unconditional) 
    { 
    } 
    public void commandAction(Command command, Displayable displayable) 
    { 
    if (command == exit) 
    { 
     destroyApp(false); 
     notifyDestroyed(); 
    } 
    else if (command == start) 
    { 
     new Thread(this).start(); 

    } 
    } 
} 

正如你所看到的,我試着寫在文本文件中的東西,從仿真器。我在一個單獨的線程中運行該代碼,以避免運行時的警告。我已經在C:\ Program Files \ WTK2.5.2_01 \ j2mewtk_template \ appdb \ DefaultColorPhone \ filesystem \ root1 \下創建一個名爲fisier.txt的文件。當我嘗試運行此代碼並按'開始'時,我在'J2ME'問題上點擊'是'... Midlet Suite希望編寫本地文件系統。可以更新文件嗎?是/否」。我在屏幕上得到了java.io.IOException:,沒有其他的東西了......

怎麼了?爲什麼我有這個錯誤?我沒有發現任何地方的工作代碼,如何寫入本地.txt文件。
不知道我的代碼有什麼問題嗎?

+0

當您捕捉到異常時,請調用Throwable.printStackTrace()以確保知道實際拋出的方法。使用Connector.READ_WRITE。確保您的模擬器安全策略在運行未簽名的MIDlet(我假設您是)時提供對文件系統的完全訪問權限。首先關閉PrintStream。用結果更新你的問題。 –

回答

1

可能是導致路徑錯誤(並且文件不存在)的任何事情,對於您沒有寫入訪問權限的情況,可能會在其他位置打開。

你有沒有打過電話的一些方法的FileConnection類的優惠,如canWrite()exists(),並isOpen(),看看其中的一些常見的問題,你的情況適用?

+0

我想: 公共無效的run(){ \t嘗試{ \t \t javax.microedition.io.file.FileConnection filecon =(javax.microedition.io.file.FileConnection) \t \t Connector.open(「文件:///root1/photos/fisier.txt「,Connector.READ_WRITE); \t \t System.out.println(filecon.exists()); 我弄錯了。但是那個文件存在! (而且是空的)。爲什麼我在那裏弄虛作假? – qwerty

+0

爲什麼路徑不正確?我應該如何重寫路徑? – qwerty

+0

我不知道你的模擬器保存你的文件。嘗試使用filecon.create()創建文件,然後搜索文件系統以查看文件放置的位置。 – funkybro

相關問題