import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FileOperations extends MIDlet implements CommandListener {
Form form = new Form("FileOperations");
StringBuffer sb = new StringBuffer();
String filePath = "file:///e:/1.txt";// 要读写的文件路径 索爱、诺基亚等存储卡
// String filePath = "file:///root1/1.txt";// 要读写的文件路径 模拟器
// StringItem si = new StringItem(null, "要写入的数据:");
StringItem info = new StringItem(null, null);
TextField tf = new TextField("要写入的数据", "测试", 100, TextField.ANY);
Command exit = new Command("退出", Command.EXIT, 1);
Command write = new Command("写文件", Command.OK, 1);
Command read = new Command("读文件", Command.OK, 1);
public FileOperations() {
// form.append(si);
form.append(tf);
form.addCommand(write);
form.addCommand(read);
form.addCommand(exit);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(form);
}
/**
* 添加文字
*
* @param str
* 要添加的文字
*/
private void addStr(String str) {
sb.append(str + "/n");
info.setText(sb.toString());
}
/**
* 写文件
*
* @param b
*/
private void write(byte[] b) {
addStr("路径为" + filePath);
write(filePath, b);
}
private void write(String path, byte[] b) {
addStr("写文件");
FileConnection fc = null;
OutputStream os = null;
try {
fc = (FileConnection) Connector.open(path);
if (!fc.exists())// 若文件不存在
fc.create();// 创建文件
else
fc.truncate(0);// 清空文件数据
os = fc.openOutputStream();
os.write(b);// 写入文件数据
os.close();
os = null;
addStr("写文件完毕");
fc.close();
fc = null;
} catch (IOException e) {
e.printStackTrace();
addStr("写文件出现异常" + e.toString());
} catch (SecurityException e) {
e.printStackTrace();
addStr("写文件时出现权限错误");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
os = null;
}
if (fc != null) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
fc = null;
}
}
}
/**
* 读文件
*
*/
private void read() {
addStr("路径为" + filePath);
read(filePath);
}
private void read(String path) {
addStr("读文件");
FileConnection fc = null;
InputStream is = null;
try {
fc = (FileConnection) Connector.open(path);
if (!fc.exists()) {// 若文件不存在
addStr("文件不存在");
return;
}
is = fc.openInputStream();
byte[] b = new byte[(int) fc.fileSize()];
is.read(b);// 读出文件数据
addStr("文件内容:");
addStr(new String(b));
addStr("读文件完毕");
is.close();
is = null;
fc.close();
fc = null;
} catch (IOException e) {
e.printStackTrace();
addStr("读文件出现异常" + e.toString());
} catch (SecurityException e) {
e.printStackTrace();
addStr("读文件时出现权限错误");
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
is = null;
}
if (fc != null) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
fc = null;
}
}
}
/**
* 清空信息
*
*/
private void cls() {
sb.delete(0, sb.length());
form.deleteAll();
// form.append(si);
form.append(tf);
form.append(info);
}
public void commandAction(Command c, Displayable arg1) {
if (c == write) {// 写文件
new Thread() {
public void run() {
cls();
write(tf.getString().getBytes());
}
}.start();
} else if (c == read) {// 读文件
new Thread() {
public void run() {
cls();
read();
}
}.start();
} else if (c == exit) {// 退出
destroyApp(true);
notifyDestroyed();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kf156/archive/2009/09/30/4621413.aspx