Android开发实现读取Assets下文件及文件写入存储卡的方法
作者:jia635 时间:2023-02-07 15:42:21
本文实例讲述了Android开发实现读取Assets下文件及文件写入存储卡的方法。分享给大家供大家参考,具体如下:
调用一个反编译的.so文件,查看起加密和解密情况,需要解析上万的数组,而so文件加密解密都是通过Byte来进行,又需要把String字符串转化为 Byte,当把数据直接写在代码中就会提示多Byte数组过大。最后把数组写到Assets文件加下,读取txt文本文件。
读取Assets方法如下:
public String getFromAssets(String fileName) {
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
然后
String strEn = getFromAssets("encode.txt");
txt中的文本文件是str,str,str这种形式,然后把
String[] encode1 = strEn.split(",");
通过字符串把 读取的字符串转化成字符串数组。
for(int i=0;i<encode1.length;i++){
sendString = encode1[i];
// sbuf.append(sendString+",");
try {
sendBytes = sendString.getBytes("UTF8");
byte[] s = Base64Encoder.B64Encode(sendBytes);
str = new String(s, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sbuf.append(str);
sbuf.append(",");
}
String.split
具有分割数组的作用,它已某一个特殊符号为分界点然后进行数组分割。
再把加密后的字符串写到本地文件。方法如下
public String saveInfo2File(String mString) {
StringBuffer sb = new StringBuffer();
try {
long timestamp = System.currentTimeMillis();
String fileName = "encut" + ".txt";
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String spath = Environment.getExternalStorageDirectory()
.getPath() + "/A1/";
File sdir = new File(spath);
if (!sdir.exists())
sdir.mkdirs();
FileOutputStream fos = new FileOutputStream(spath + fileName);
sb.append(mString);
fos.write(sb.toString().getBytes());
fos.close();
}
return fileName;
} catch (Exception e) {
}
return null;
}
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/jia635/article/details/40591373
标签:Android,Assets
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android热修复Tinker接入及源码解读
2023-11-14 09:27:18
![](https://img.aspxhome.com/file/2023/9/138629_0s.jpg)
React-Native之Android(6.0及以上)权限申请详解
2023-07-28 01:56:55
![](https://img.aspxhome.com/file/2023/8/85998_0s.jpg)
学习Java的Date、Calendar日期操作
2023-09-04 22:26:38
WPF使用Geometry绘制几何图形
2023-08-14 14:13:31
![](https://img.aspxhome.com/file/2023/7/108717_0s.gif)
Java 8中字符串拼接新姿势StringJoiner详解
2022-03-09 03:00:28
解决MyEclipse10.7部署报错抛空指针异常问题的方法
2023-10-14 23:52:34
![](https://img.aspxhome.com/file/2023/5/82315_0s.jpg)
android 一些工具类汇总
2023-08-29 11:32:05
Java俄罗斯方块小游戏
2021-12-01 04:36:49
![](https://img.aspxhome.com/file/2023/1/65581_0s.png)
Android广播接收机制详细介绍(附短信接收实现)
2023-04-16 10:09:57
SpringBoot上传文件到本服务器 目录与jar包同级问题
2022-01-27 07:15:41
![](https://img.aspxhome.com/file/2023/9/84199_0s.jpg)
Android实现自定义曲线图
2023-08-22 07:05:01
![](https://img.aspxhome.com/file/2023/0/137700_0s.jpg)
Android播放多张图片形成的一个动画示例
2021-08-23 18:07:32
Java基于正则实现的日期校验功能示例
2021-09-30 02:43:26
Android仿支付宝中余额宝的数字动画效果
2021-10-17 08:32:54
![](https://img.aspxhome.com/file/2023/1/137721_0s.gif)
java String的intern方法
2021-07-05 03:23:52
JDK源码之Vector与HashSet解析
2021-09-06 10:47:23
![](https://img.aspxhome.com/file/2023/4/64144_0s.png)
springboot vue测试平台开发调通前后端环境实现登录
2021-08-13 03:54:02
![](https://img.aspxhome.com/file/2023/9/64419_0s.png)
SpringAOP如何获取方法参数上的注解
2021-09-19 23:22:18
![](https://img.aspxhome.com/file/2023/3/73123_0s.png)
c# 断点续传的实现
2023-07-01 22:41:02
初步了解javafx
2021-09-30 16:20:11
![](https://img.aspxhome.com/file/2023/1/96061_0s.jpg)