yiweifeng 发表于 2013-2-7 09:58:31

[J2ME]文件操作

文件操作 一

<div class="postbody">1. JSR75方式
Jsr75分两个部分,一部分是File,即本地文件系统,一部分是PIM,就是电话本之类的信息,我先学习的是File部分,通过import javax.microedition.io.file.*里的包,可以实现自由访问本机的文件系统,就跟Windos里的资源管理器一样,在这里简单介绍一下:

         首先你必须检查选定的设备是否支持FileConnection。通过如下方式:


...
// Check that the File Connection Optional Package is there

String v = System.getProperty("microedition.io.file.FileConnection.version" );

if( v != null ){
 // FCOP available
} else {
 // FCOP not available
}
...
  如果v返回为null那么说明不支持FCOP,否则应该返回版本号,比如1.0。


一.获取指定路径的目录和文件列表/*目录文件列表*/    public Vector list(String path)
    {
       try
       {
           FileConnection fc = (FileConnection) (Connector.open(path));
 
           if (fc.exists())
           {
              Vector listVec = new Vector(0, 1);
 
              Enumeration en = fc.list();
 
              while (en.hasMoreElements())
              {
                  listVec.addElement((String) (en.nextElement()));
              }
 
              return listVec;
           }
           else
           {
              return null;
           }
       }
       catch (Exception e)
       {
           System.out.println("listErr:" + e.toString());
           return null;
       }
    }
 
 

方法里的path参数就是要查找的路径,比如:file://localhost/root1/test.txt/
这里需要注意的是:这个文件必须在模拟器的文件路径下.有时候模拟器启动的临时文件夹, 还需要确保临时文件夹下面有test.txt文件存在
二.建立/保存/删除文件
/*保存文件*/
    public void saveFile(String path, byte[] fileData)
    {
       try
       {
           FileConnection fc = (FileConnection) (Connector.open(path));
 
           fc.create();
 
           fc.setWritable(true);
 
           OutputStream os = fc.openOutputStream();
 
           os.write(fileData);
 
           os.close();
       }
       catch (Exception e)
       {
           System.out.println("saveFileErr:" + e.toString());
       }
    }
 

 
/*删除文件*/
    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());
       }
    }
 


/*读取文件*/
    public byte[] readFile(String path)
    {
       try
       {
           FileConnection fc = (FileConnection) (Connector.open(path));
          
           if (fc.exists())
           {
              InputStream is = fc.openInputStream();
             
              byte[] temp = new byte;
             
              is.read(temp);
             
              is.close();
             
              return temp;
           }
           else
           {
              return null;
           }
       }
       catch (Exception e)
       {
           System.out.println("readFileErr:" + path + e.toString());
          
           return null;
       }
    }
 

2. getResourceAsStream方式

这种方式实现的方式把要读取的文件放到工程res文件夹(其实很多文件夹都可以放), 然后一起打包进jar, 程序运行时把整个文件都读入到内存, 这样就带来一个问题, 这个文件不能太大, 否则程序运行时会报OutOfMemory异常. 这种方式只能对资源文件进行读操作.

InputStream is = null;
 
DataInputStream dis = null;
 
is = this.getClass().getResourceAsStream("/20080304_111846.mp4");
 
if (is == null)
{
    return false;
}  
int hasReadLen = dis.read(dataBuf, 0, readLen);
dis = new DataInputStream(is);
 
 
页: [1]
查看完整版本: [J2ME]文件操作