下面是我写文件写入代码,为什么不能创建文件呢,单步调式到红色标记是就过不去了
package file;
import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.io.file.FileConnection;
public class Filetest extends MIDlet implements CommandListener,Runnable {
private Display display;
private Command exit = new Command("Exit",Command.EXIT,1);
private Command start = new Command("Start",Command.SCREEN,1);
private Form form;
public Filetest() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
form = new Form("fileTest");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() {
// TODO Auto-generated method stub
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d){
if(c == exit){
destroyApp(false);
notifyDestroyed();
}
if(c == start){
new Thread(this).start();
}
}
public void run() {
String path = "file:///root1/test.txt";// 模拟器上的路径
byte[] b = "hello world".getBytes();// 这里先写入hello world
try {
// path为文件路径及文件名
FileConnection fc = (FileConnection) Connector.open(path,Connector.READ_WRITE);
if (!fc.exists())// 若文件不存在,创建文件
fc.create();
OutputStream os = fc.openOutputStream();// 打开输出流
os.write(b);// b为byte[] 要写入的文件数据
os.close();
fc.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
package file;
import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.io.file.FileConnection;
public class Filetest extends MIDlet implements CommandListener,Runnable {
private Display display;
private Command exit = new Command("Exit",Command.EXIT,1);
private Command start = new Command("Start",Command.SCREEN,1);
private Form form;
public Filetest() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
form = new Form("fileTest");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() {
// TODO Auto-generated method stub
display.setCurrent(form);
}
public void commandAction(Command c,Displayable d){
if(c == exit){
destroyApp(false);
notifyDestroyed();
}
if(c == start){
new Thread(this).start();
}
}
public void run() {
String path = "file:///root1/test.txt";// 模拟器上的路径
byte[] b = "hello world".getBytes();// 这里先写入hello world
try {
// path为文件路径及文件名
FileConnection fc = (FileConnection) Connector.open(path,Connector.READ_WRITE);
if (!fc.exists())// 若文件不存在,创建文件
fc.create();
OutputStream os = fc.openOutputStream();// 打开输出流
os.write(b);// b为byte[] 要写入的文件数据
os.close();
fc.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}