2010-03-07 4 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 \ photos에 fisier.txt라는 파일이 있습니다. 이 코드를 실행하고 'Start'키를 누르면 'J2ME ... Midlet Suite가 로컬 파일 시스템을 작성하려고합니다. 파일을 업데이트해도 괜찮습니까? 예 아니오'. 그리고 화면에 나타났습니다 java.io.IOException :, 그 외 아무것도 없습니다! ..

무엇이 잘못 되었습니까? 왜 내가 그 오류가있어? 어디서나 코드를 사용하여 로컬 .txt 파일에 쓰는 방법을 찾지 못했습니다.
내 코드에서 무엇이 잘못되었는지 알 수 없습니까?

+0

예외를 catch 할 때 Throwable.printStackTrace()를 호출하여 실제로 어떤 메서드에서 throw하는지 알아야합니다. Connector.READ_WRITE를 사용하십시오. 서명되지 않은 MIDlet (사용자의 것으로 가정)을 실행할 때 에뮬레이터 보안 정책이 파일 시스템에 대한 모든 액세스 권한을 제공하는지 확인하십시오. PrintStream을 먼저 닫습니다. 결과로 질문을 업데이트하십시오. –

답변

1

경로가 잘못되어 (파일이 존재하지 않음), 쓰기 권한이없는 사용자가 다른 곳에 열려있을 수 있습니다.

는 이러한 일반적인 문제 중 일부는 귀하의 경우에 적용되는지, 같은 canWrite(), exists()isOpen() 같이 메소드 FileConnection 클래스 제공의 일부를 호출하려고 했습니까?

+0

내가 시도 : 공공 무효 실행() { \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

관련 문제