⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystem.java

📁 自建的一个通讯录
💻 JAVA
字号:
package com.north.phonebook.model;

//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);
			  int listType = PIM.CONTACT_LIST;
			  String listName = "Phone";
			   if(true)
			   {
				   return pim;
			   }
				  
			  PIMList list = PIM.getInstance().openPIMList(listType, PIM.READ_ONLY, listName);
			  for (Enumeration items = list.items(); items.hasMoreElements();)
			  {
				  PIMItem item = (PIMItem) items.nextElement();
				  String name = item.getString(Contact.FORMATTED_NAME, 0);
				  String phone = item.getString(Contact.TEL, 0);
				  pim.addElement(new String[]{name,phone});
			  } 			  
			  
			  /*
			   Vector pim=new Vector(0,1);
			   ContactList cl=(ContactList)(PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY, "Phone"));
			   if(true)
			   {
				   return pim;
			   }
			   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});
				    }
			   }
			   */
			   return pim;
		  }
		  catch(Exception e)
		  {
			   System.out.println(e);
			   message="读取PIM失败:"+e.toString();
			   return null;
		  }
	 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -