Android读取工程根目录下文件内容

转自:http://blog.csdn.net/z_sawyer/article/details/17918371 
众所周知,安卓某些资源目录,RAW目录以及ASSETS目录下的文件都能轻易读取,但是工程根目录下的文件Android确没有提供方法读取,只能自己想办法,曲线救国了android应用的后缀名称为.apk其实就是一个压缩文件,可以用解压缩工具查看里面的文件信息,那我想也可以通过读取压缩包文件的方式读取工程根目录下的文件的内容,android也提供了读取压缩文件信息的接口。那么就需要找到这个压缩包的存在,还好android提供了一个方法可以读取到apk的信息context.getPackageCodePath(),下面是详细代码:

private static File findFile(Context context) {  
        String str3 = context.getPackageCodePath();  
        try {  
            ZipInputStream zipInput=new ZipInputStream(new FileInputStream(str3));  
            ZipEntry currentZipEntry = null;  
            while ((currentZipEntry=zipInput.getNextEntry())!=null) {  
                String name = currentZipEntry.getName();  
                if (!currentZipEntry.isDirectory()) {  
                    Log.d("zengnengxin", name + "is a normal file");    
                    if( name.equalsIgnoreCase("AndroidManifest.xml")){  
                        File file = new File(context.getFilesDir() + File.separator + name);    
                        file.createNewFile();    
                        // get the output stream of the file    
                        FileOutputStream out = new FileOutputStream(file);    
                        int ch;    
                        byte[] buffer = new byte[1024];    
                        //read (ch) bytes into buffer    
                        while ((ch = zipInput.read(buffer)) != -1){    
                            // write (ch) byte from buffer at the position 0    
                            out.write(buffer, 0, ch);    
                            out.flush();    
                        }    
                        out.close();    
                        return file;  
                    }  
                }  
            }  
            zipInput.close();  
        } catch (Exception e) {  
            // TODO: handle exception  
        }     
        return null;  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值