[J2ME]文件操作

本文介绍了J2ME中JSR75规范下的文件操作方法,包括如何检查设备是否支持FileConnection、获取指定路径下的目录及文件列表、创建/保存/删除文件等,并对比了getResourceAsStream方式的特点。

[J2ME]文件操作 一

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.available()];

             

              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);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值