手机读取存储卡上的文件问题

时间:2008-08-27 23:01:40   来源:论坛整理  作者:  编辑:chinaitzhe
我在手机存储卡上的docoment文件夹放了一个test.txt
然后
fc=(FileConnection) Connector.open("file:///e:/docoment/test.txt", Connector.READ_WRITE);
DataInputStream dis=fc.openDataInputStream();
int size=dis.available();
byte data[]=new byte[size];
dis.read(data);
dis.close();
fc.close();
string s=new String(data,"UTF-8");
之后显示出s ,但是什么也没有显示为什么呢?



网友回复:int size=dis.available();
这一句我调试的是有问题的,返回是0

取大小建议用 int size=(int)fc.fileSize();
这是我写的,测试通过
static FileConnection fc;
static byte [] txtMess;
public static String readTxt(String des) {
txtMess = null;
fc = null;
try {
fc=(FileConnection)Connector.open(des, Connector.READ_WRITE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fc.exists()){
try {
DataInputStream ips=fc.openDataInputStream();
int length = (int)fc.fileSize();
txtMess = new byte[length];
System.out.println("" length);
ips.read(txtMess,0,length);
System.out.println("ower*********");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String reString;
reString = new String(txtMess);
return reString;
}
你试试,你要捕捉下异常哈。确保在真机上没异常发生.
关键字:文件,问题,手机,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面