次のコードがあります。
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 という名前のファイルがあります。このコードを実行して [開始] を押すと、[J2ME... Midlet Suite はローカル ファイル システムに書き込みたい] という質問に対して [はい] をクリックします。ファイルを更新してもよろしいですか?はい・いいえ'。そして、私は画面java.io.IOException:に乗りました、そしてそれ以上のものはありません!..
どうしたの?なぜそのエラーが発生したのですか?ローカルの .txt ファイルに書き込む方法の作業コードはどこにも見つかりませんでした。
私のコードの何が問題なのかわからない?