J2ME 本地文件读写

转载http://www.devdiv.net/viewthread-15766

发布: 2009-9-30 15:32 | 作者: kf156 | 来源: DevDiv 移动开发社区

 

 

 

测试前请确认手机 支持JSR75,且拥有文件 读写权限。Demo里的文件路径为诺基亚 、索爱的存储卡,模拟 器或其他品牌手机请改为相应的路径。


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;
/**
* 文件读写Demo
*
* @author kf156(亚日)
*
*/
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();
  }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值