나는 다음과 같은 코드가 있습니다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 파일에 쓰는 방법을 찾지 못했습니다.
내 코드에서 무엇이 잘못되었는지 알 수 없습니까?
예외를 catch 할 때 Throwable.printStackTrace()를 호출하여 실제로 어떤 메서드에서 throw하는지 알아야합니다. Connector.READ_WRITE를 사용하십시오. 서명되지 않은 MIDlet (사용자의 것으로 가정)을 실행할 때 에뮬레이터 보안 정책이 파일 시스템에 대한 모든 액세스 권한을 제공하는지 확인하십시오. PrintStream을 먼저 닫습니다. 결과로 질문을 업데이트하십시오. –