Java me J2ME java.io.IOException错误
我有以下代码:Java me J2ME java.io.IOException错误,java-me,midlet,midp,Java Me,Midlet,Midp,我有以下代码: 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; priva
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:\ProgramFiles\WTK2.5.2\U 01\j2mewtk\U template\appdb\DefaultColorPhone\filesystem\root1\photos中有一个名为fisier.txt的文件。当我试着运行这段代码并按“开始”时,我在问题“J2ME。。。Midlet套件想要编写本地文件系统。可以更新你的文件吗?是/否'。我在屏幕上看到了java.io.IOException:,除此之外就什么都没有了
怎么了?为什么我会犯那个错误?我在任何地方都找不到关于如何写入本地.txt文件的工作代码。不知道我的代码出了什么问题?可能是路径错误(文件不存在)、您没有写入权限、文件在其他地方被打开
您是否尝试调用
FileConnection
类提供的一些方法,例如canWrite()
、exists()
、和isOpen()
,以查看这些常见问题是否适用于您的情况?我尝试过:public void run(){try{javax.microedition.io.file.FileConnection filecon=(javax.microedition.io.file.FileConnection)Connector.open(“file:///root1/photos/fisier.txt,Connector.READ_WRITE);System.out.println(filecon.exists()),我得到了'false'。但该文件存在!(并且是空的)。为什么我在那里得到了false?我不知道模拟器将文件保存在哪里。请尝试使用filecon.create()创建该文件,然后搜索文件系统以查看文件放置的位置。捕获异常后,调用Throwable.printStackTrace()以确保知道实际抛出该异常的方法。使用Connector.READ_WRITE。确保在运行未签名的MIDlet(我假定您的MIDlet是)时,仿真器安全策略允许完全访问文件系统。首先关闭打印流。使用结果更新您的问题。