1. 读取本地文件
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class LoadText
extends MIDlet implements CommandListener {
private Display display;
private Form mainForm;
private TextField textField;
public void startApp() {
this.display = Display.getDisplay(this);
this.mainForm = new Form("本地ANSI).编码的文本");
this.textField = new TextField("靠自己", "hello", 10240, TextField.ANY);
this.mainForm.addCommand(new Command("离开", Command.EXIT, 0));
String str = loadText("/kaoziji(ANSI).txt");
this.textField.setString(str);
this.mainForm.append(this.textField);
this.mainForm.setCommandListener(this);
this.display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
this.notifyDestroyed();
}
//读取本地ANSI编码文本(没问题)
private String loadText(String yourfilename) {
InputStream is = null;
String strs = "";
try {
Class c = this.getClass();
is = c.getResourceAsStream(yourfilename);
InputStreamReader isr = new InputStreamReader(is);
StringBuffer buffer = new StringBuffer();
int ch;
while ( (ch = isr.read()) > -1) {
buffer.append( (char) ch);
}
strs = buffer.toString();
if (isr != null) {
isr.close();
}
if (buffer != null) {
buffer = null;
}
}
catch (IOException e) {
}
return strs;
}
}=======================================
//读取本地UTF-8编码的文本文件(没问题)
public String loadText(String name) {
String strReturn = "";
InputStream in = null;
byte[] word_utf = new byte[1024];
try {
in = getClass().getResourceAsStream(name);
in.read(word_utf);
in.close();
strReturn = new String(word_utf, "UTF-8");
}
catch (Exception e) {
System.out.println("读取本地UTF-8编码的文本出错:" + e.toString());
}
finally {
in = null;
}
return strReturn;
}========================================
//读取本地Unicode big endian编码文本文件(没问题)
public String loadText(String name) {
String strReturn = "";
InputStream in = null;
byte[] word_utf = new byte[1024];
try {
in = getClass().getResourceAsStream(name);
in.read(word_utf);
in.close();
strReturn = new String(word_utf, "UTF-16BE"); //后一个参数改为"UTF-8"即可读取UTF-8编码的
}
catch (Exception e) {
System.out.println(e.toString());
}
finally {
in = null;
}
return strReturn;
}================================
//读取Unicode big endian编码文本(有问题)
private String loadText(String resource) {
char[] word_uni_b_e = new char[1024];
String strReturn = "";
DataInputStream dis;
try {
dis = new DataInputStream(getClass().getResourceAsStream(resource));
int counter = 0;
dis.skip(2);
char temp;
while (true) {
temp = dis.readChar();
if (temp == ') {
break;
}
word_uni_b_e[counter++] = temp;
}
dis.close();
strReturn = String.valueOf(word_uni_b_e, 0, counter);
} catch (Exception e) {
System.out.println("读取Unicode big endian编码文本文件出错!" + e.getMessage());
}
finally {
dis = null;
}
return strReturn;
} 读取Unicode big endian编码文本文件出错!null
不知何因
====================================
//读取ANSI编码文本 (有问题)
private String loadText(String resource) {
byte[] word_uni_b_e = new byte[10240];
DataInputStream dis;
try {
dis = new DataInputStream(getClass().getResourceAsStream(resource));
dis.readFully(word_uni_b_e);
}
catch (Exception e) {
System.out.println("读取ANSI编码文本文件出错!" + e.getMessage());
}
finally {
dis = null;
}
return new String(word_uni_b_e);
}
2.读取WEB上的文本文件方法
使用Connector类的以下静态方法创建远程连接对象:
然后读取文本数据至客户端
下面的dis.read()函数都可以改为
readFully(byte[] b) ,readFully(byte[] b, int off, int len)
,read(byte[] b, int off, int len)
==============================================================
Connection con = Connector.open(String url);
Connection con = Connector.open(String url,int mode);
Connection con = Connector.open(String url,int mode,boolean timeouts);
DataInputStream dis = Connector.openDataInputStream(String url);
InputStream is = Connector.openInputStream(String url);
<1.
DataInputStream dis = Connector.openDataInputStream(url);
byte[] data = new byte[length];//数组大小按情况而定
dis.read(data);
return new String(data);
<2.
HttpConnection hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();
byte[] data = new byte[length];
DataInputStream dis = new DataInputStream(hpc.openInputStream());
dis.read(data);
return new String(data);
<3.
HttpConnection hpc = (HttpConnection)Connector.open(url);
int length = (int)hpc.getLength();
byte[] data = new byte[length];
DataInputStream dis = = hpc.openDataInputStream();
dis.read(data);
return new String(data);
<4.
InputStream is = Connector.openInputStream(url);
byte[] data = new byte[length];//数组大小按情况而定
is.read(data);
return new String(data);
<5.
StreamConnection c = (StreamConnection)Connector.open(url);
InputStream is = c.openInputStream(url);
byte[] data = new byte[length];//数组大小按情况而定
is.read(data);
return new String(data);
<6.
StreamConnection sc = (StreamConnection)Connector.open(url);
DataInputStream dis = = sc.openDataInputStream();
byte[] data = new byte[length];//数组大小按情况而定
dis.read(data);
return new String(data);
<7.
InputStream is = Connector.openInputStream(url);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while((ch = is.read()) != -1)
bytestream.write(ch);
String str = new String(bytestream.toByteArray());
bytestream.close();
return new String(str);
<8.(乱码)
InputStream is = Connector.openInputStream(url);
StringBuffer buffer=new StringBuffer();
int ch;
while((ch = is.read()) != -1)
buffer.append((char)ch);
String str = new String(buffer.toString());
return new String(str);
3.读取Web上的图片(png,jpg,gif)
package com.karant.readWebImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
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.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class LoadImageMIDlet extends MIDlet implements Runnable,CommandListener{
private Display display;
private Thread imageThread;
private Item mItem;
private Form mainForm,imageForm;
public LoadImageMIDlet() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
mainForm = new Form("图片正在加载,请稍等...");
imageForm = new Form("下载的图片");
imageForm.addCommand(new Command("退出", Command.EXIT, 0));
imageForm.setCommandListener(this);
display.setCurrent(mainForm);
if(mItem == null)
{
imageThread = new Thread(this);
imageThread.start(); // 启动线程,调用Runnable接口的run()方法
}
}
public void commandAction(Command c, Displayable d) {
this.notifyDestroyed();
}
public void run() { //实现Runnable中的抽象方法run()方法
try {
String URL = "http://hdn.xnimg.cn/photos/hdn521/20110510/1740/h_large_HyoW_55d400005be12f74.jpg";
Image image = loadImage(URL); //获取服务器图片
mItem = new ImageItem(null, image, 0, null); //参数:标签;图像,必须是不可变的;布局(0-3) ;替代图像的文本
} catch (IOException e) {
mItem = new StringItem(null, e.toString());
}
/*加一个包含图像的item对象到form中
*其效果相当于调用append(new ImageItem(null,img,ImageItem.LAYOUT_DEFAULT,null)方法
*如果参数img为Null,会抛出NUllPointException
*如果参数img是可变图像,会抛出lllegalArgumentException
*
*/
imageForm.append(mItem);
display.setCurrent(imageForm);
}
// //获取服务器图片
// public Image loadImage(String url) throws IOException
// {
// HttpConnection hpc = null;
// DataInputStream dis = null;//
// try
// {
// //打开一个连接返回一个Connection接口类的对象,
// //之后转型为HttpConnection接口类的对象
// hpc = (HttpConnection)Connector.open(url);
// int length = (int)hpc.getLength();//获取数据长度
// byte[] data = new byte[length];//创建一个字节数组对象
//
// //openInputStream()打开并返回一个输入流
// //DataInputStream类对象:
// //数据输入流允许应用程序以与机器无关方式从基础输入流中读取基本 Java 数据类型。
// //应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
// dis = new DataInputStream(hpc.openInputStream());
//
// //从输入流(dis)中读取一些字节,并将它们存储到缓冲区数组 data中。
// //读取的字节数等于 data 的长度
// dis.readFully(data);
//
// //用于创建一个用字节数组存储的不可变图像。
// //数组中存放的图像必须是系统(J2ME系统环境)支持的图像格式
// //比如PNG格式
// //该方法主要用于从外部资源中装入图像,比如数据库和网络中
// //三个参数表示:
// //存储的系统支持格式的图像字节数组;
// //起始数据的位置(范围0-data.length-1);
// //数据的长度(0-data.length);
// //原型为:
// //public static Image createImage(byte[] imageData,int imageOffset,int imageLength)
// return Image.createImage(data,0,data.length);
// }
// finally
// {
// //close()方法用于关闭连接
// //当连接被关闭后,除了该方法外,其他任何方法的调用都会
// //抛出IOException异常
// //关闭一个已经关闭的连接不会产生任何影响
// if(hpc != null) hpc.close();
// if(dis != null) dis.close();
// }
// }
//另外一中方法
public Image loadImage(String url) throws IOException
{
InputStream is = Connector.openInputStream(url);//输入流连接地址
byte[] imagearray = null; //存储数据的数组
ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); //输出流,获得数据
try
{
int ch;
while((ch = is.read()) != -1) //将输入流的数据读出
bytestream.write(ch);
imagearray = bytestream.toByteArray(); //将输出流得到的数据存进数据
return Image.createImage(imagearray,0,imagearray.length); //返回创建好的不可变数组图片
}
finally
{
if(is != null) is = null;
if(bytestream != null) bytestream = null;
if(imagearray != null) imagearray = null;
}
}
}