jsr75相关操作代码新版本
这个版本比之前那个代码多了如下内容:
1.增加了机型判断,增加了两个方法用以直接读取手机的图片文件和铃声文件
2.列表方法增加了根目录查看
3.完善了PIM读取方法,只读取电话本里的联系人姓名和电话
4.完善了写文件操作,在非强制写入的情况下做了异常判断
如下代码里的注释部分是我自己用来调试用的,可以不要
/*文件系统类
开发:李果
开发开始日期:2006年12月11日
*/
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.io.*;
import java.util.*;
import javax.microedition.pim.*;
public class fileSystem
{
int fileSize;//文件总大小
int currentProgress;//读取进度
String platform;
String message;
String imageFilePath;
String soundFilePath;
public static final String S40_DEVICE[]={"2610","2855","2865","3152","3155","5140","5200","5300","6020","6060","6080","6085","6101","6102","6103","6111","6125","6126","6131","6133","6136","6151","6152","6155","6155","6165","6170","6230","6233","6234","6235","6255","6265","6270","6280","6282","6288","6822","7260","7270","7360","7370","7373","7390","8800",};
public fileSystem()
{
platform = System.getProperty("microedition.platform");
if(platform.startsWith("SonyEricsson"))
{
imageFilePath="c:/pictures/";
soundFilePath="c:/sounds/";
}
else
if(platform.startsWith("Nokia"))
{
boolean isS40=false;
for(int i=0;i<S40_DEVICE.length;i++)
{
if(platform.startsWith("Nokia".concat(S40_DEVICE[i])))
{
isS40=true;
break;
}
}
if(isS40)
{
imageFilePath="C:/predefgallery/predefphotos/";
soundFilePath="C:/predefgallery/predefrecordings/";
}
else
{
imageFilePath="C:/Data/Images/";
soundFilePath="C:/Data/Sounds/Digital/";
}
}
else
{
imageFilePath="/";
soundFilePath="/";
}
}
/**保存文件
* @path:路径
* @fileData:文件数据
* @overrite:是否强制覆盖保存
* @return: -1:文件已存在,0:出现异常,1:保存成功
*/
public int saveFile(String path,byte[] fileData,boolean overrite)
{
try
{
FileConnection fc=(FileConnection)(Connector.open(path));
if(fc.exists())
{
if(overrite)
{
fc.create();
fc.setWritable(true);
OutputStream os=fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
}
else
return -1;
}
else
{
fc.create();
fc.setWritable(true);
OutputStream os=fc.openOutputStream();
os.write(fileData);
os.close();
return 1;
}
}
catch(Exception e)
{
System.out.println("saveFileErr:"+e.toString());
return 0;
}
}
/*删除文件*/
public void deleteFile(String path)
{
try
{
FileConnection fc=(FileConnection)(Connector.open(path));
if(fc.exists())
fc.delete();
}
catch(Exception e)
{
System.out.println("deleteFileErr:"+e.toString());
}
}
/**获取目录文件列表
* @path:路径
* @type:列表类型(0:文件夹和文件都列;1:只列文件夹;2:只列文件)
* @return:指定路径下的目录和文件的名称列表
*/
public Vector getList(String path,int type)
{
try
{
Enumeration en=null;
Vector listVec=new Vector(0,1);
if(path.equals("root"))
{
en=FileSystemRegistry.listRoots();
}
else
{
FileConnection fc=(FileConnection)(Connector.open(path));
if(fc.exists())
en=fc.list();
}
if(en!=null)
{
while(en.hasMoreElements())
{
String name=(String)(en.nextElement());
switch(type)
{
case 0:
listVec.addElement(name);
break;
case 1:
if(name.indexOf("/")!=-1)
listVec.addElement(name);
break;
case 2:
if(name.indexOf("/")==-1)
listVec.addElement(name);
break;
}
}
return listVec;
}
else
{
System.out.println("pathErr");
return null;
}
}
catch(Exception e)
{
System.out.println("listErr:"+e.toString());
message=e.toString();
return null;
}
}
/*图片文件列表*/
public Vector getImageList()
{
Vector list=getList("file:///"+imageFilePath,0);
return list;
}
/*声音文件列表*/
public Vector getSoundList()
{
Vector list=getList("file:///"+soundFilePath,0);
return list;
}
/*读取文件
$path:文件路径
$showProgress:是否显示读取进度,注意,如果选择显示进度会导致读取文件速度变慢
$return:如果成功获取到文件则返回存储文件数据的数组,否则返回null
*/
public byte[] readFile(String path,boolean showProgress)
{
try
{
FileConnection fc=(FileConnection)(Connector.open(path));
if(fc.exists())
{
message="文件存在";
InputStream is=fc.openInputStream();
fileSize=is.available();
byte[] temp=new byte[fileSize];
/*如果需要显示进度*/
if(showProgress)
{
int ch;
int i=0;
int step=fileSize/100;
currentProgress=0;
while((ch=is.read())!=-1)
{
temp[i++]=(byte)ch;
if(i%step==0)
currentProgress++;
}
}
else
is.read(temp);
is.close();
return temp;
}
else
return null;
}
catch(Exception e)
{
System.out.println("readFileErr:"+path+e.toString());
message=e.toString();
return null;
}
}
/**获取手机中的联系人信息
* @return:包含联系人姓名和电话的Vector
*/
public Vector readPIM()
{
try
{
Vector pim=new Vector(0,1);
ContactList cl=(ContactList)(PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY));
Enumeration en=cl.items();
Vector c=new Vector(0,1);
while(en.hasMoreElements())
{
c.addElement((Contact)(en.nextElement()));
}
System.out.println(c.size());
for(int i=0;i<c.size();i++)
{
Contact temp=(Contact)(c.elementAt(i));
if(cl.isSupportedField(Contact.TEL))
{
String name="",phone="";
if(cl.isSupportedField(Contact.FORMATTED_NAME))
{
for(int m=0;m<temp.countValues(Contact.FORMATTED_NAME);m++)
name+="&"+temp.getString(Contact.FORMATTED_NAME,m);
}
name+="&";
for(int m=0;m<temp.countValues(Contact.TEL);m++)
phone+="&"+temp.getString(Contact.TEL,m);
phone+="&";
pim.addElement(new String[]{name,phone});
}
//pim.addElement((String)(temp.getString(Contact.FORMATTED_NAME,m)));
//System.out.println(temp.getString(Contact.TEL,m));
// message="数量:"+c.size()+
// (cl.isSupportedField(Contact.NAME)?"Name:"+temp.countValues(Contact.NAME):"")
// +(cl.isSupportedField(Contact.ADDR)?"addr:"+temp.countValues(Contact.ADDR):"")
// +(cl.isSupportedField(Contact.EMAIL)?"Email:"+temp.countValues(Contact.EMAIL):"")
// +(cl.isSupportedField(Contact.FORMATTED_NAME)?"FORMATTED_NAME:"+temp.countValues(Contact.FORMATTED_NAME):"")
// +(cl.isSupportedField(Contact.NICKNAME)?"NICKNAME:"+temp.countValues(Contact.NICKNAME):"")
// +(cl.isSupportedField(Contact.TEL)?"TEL:"+temp.countValues(Contact.TEL):"")
// +(cl.isSupportedField(Contact.TITLE)?"Title:"+temp.countValues(Contact.TITLE):"");
}
return pim;
}
catch(Exception e)
{
System.out.println(e);
message="读取PIM失败:"+e.toString();
return null;
}
}
}