将可以序列化的对象通过base64编码后进行保存

但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可

package com.wotlab.home.moneyplantairs.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.HashMap; import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64; public class FileManager {
/**
* 将由网络获取的0点开始的等级数据封装成的ArrayTypeMap对象保存到sp文件中
*
* @param arrayMap
* @param sp
* @throws IOException
*/
@SuppressLint("NewApi")
public static void saveInfo(ArrayMapType arrayMap, SharedPreferences sp,
String key) throws IOException {
// 这个需要将请求到的数据保存到sp文件当中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos);
os.writeObject(arrayMap);
// 编码是将字符数组编码为字符串
String stringBase64 = new String(Base64.encodeToString(
bos.toByteArray(), Base64.DEFAULT));
Editor editor = sp.edit();
editor.putString(key, stringBase64);
editor.commit();
} @SuppressLint("NewApi")
/**
* 从sp文件中读取信息,返回ArrayList<HashMap<String,String>>类型的数据
* @param sp sp文件
* @param key sp文件中存储对象的key值
* @return
* @throws StreamCorruptedException
* @throws IOException
* @throws ClassNotFoundException
*/
public static ArrayList<HashMap<String, String>> readInfo(
SharedPreferences sp, String key) throws StreamCorruptedException,
IOException, ClassNotFoundException {
String stringBase64 = sp.getString(key, null);
// 进行对应的解码,
byte[] bytesBase64 = Base64.decode(stringBase64.getBytes(),
Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(bytesBase64);
ObjectInputStream ois = new ObjectInputStream(bais);
ArrayMapType arrayMap = (ArrayMapType) ois.readObject();
return arrayMap.list;
} }

android对象base64编码保存

sd卡文件读写工具类

这个类还是有可以改进之处的

package com.wotlab.home.moneyplantairs.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream; import android.content.Context;
import android.os.Environment; public class SDFileUtils {
private String SDPATH; // 用于存sd card的文件的路径    public String getSDPATH() {
return SDPATH;
} public void setSDPATH(String sDPATH) {
SDPATH = sDPATH;
} /**
*  构造方法,获取SD卡路径
*/
public SDFileUtils() {
// 获得当前外部存储设备的目录 
SDPATH = Environment.getExternalStorageDirectory() + "/";
} /**
* 在SD卡上创建文件
*
* @throws IOException
**/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
} /**
* 在SD卡上创建目录   
*/
public File createSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
System.out.println("storage device's state :"
+ Environment.getExternalStorageState());
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
System.out.println("this directory real path is:"
+ dir.getAbsolutePath());
System.out.println("the result of making directory:" + dir.mkdir());
}
return dir;
} /**
*  判断SD卡上的文件夹是否存在
**/
public boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
} /**
*  将一个inputSteam里面的数据写入到SD卡中   
**/
public File write2SDFromInput(String path, String fileName,
InputStream inputStream) {
File file = null;
OutputStream output = null;
try { File tempf = createSDDir(path);
System.out.println("directory in the sd card:" + tempf.exists());
file = createSDFile(path + fileName);
System.out.println("file in the sd card:" + file.exists());
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
while ((inputStream.read(buffer)) != -1) {
output.write(buffer);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}

sd卡文件读写

从asserts目录下读取文件的工具类

private String readAsserts(String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(
getAssets().open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}

asserts目录文件读写

android 文件读写工具类的更多相关文章

  1. properties文件读写工具类

    java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...

  2. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  3. list集合、txt文件对比的工具类和文件读写工具类

    工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...

  4. java文件读写工具类

    依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...

  5. java简单的文件读写工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  6. properties文件读写工具类PropertiesUtil.java

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  7. [转]Android - 文件读写操作 总结

     转自:http://blog.csdn.net/ztp800201/article/details/7322110 Android - 文件读写操作 总结 分类: Android2012-03-05 ...

  8. 文件类型工具类:FileTypeUtil

    个人学习,仅供参考! package com.example.administrator.filemanager.utils;import java.io.File;/** * 文件类型工具类 * * ...

  9. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

随机推荐

  1. window环境rabbitMQ安装和php扩展安装

    下面的安装步骤,已经在2018-11-27试验通过. 1.安装前的准备 phpinfo查看php的版本.系统是多少位的,php版本是否是线程安全,php.ini文件的路径 2.安装rabbitMQ 安 ...

  2. 华南理工大学 “三七互娱杯” G HRY and tree

    https://ac.nowcoder.com/acm/contest/874/G 题目大意:对于一个连通图,现在定义两个点的贡献为连接两点的路径上最大的权值 求任意两个点贡献的和 这个题看懂花了我很 ...

  3. CentOS服务器安装Anaconda

    今天拿到了服务器,但是需要的环境都没有,从头开始配. 需要的环境很多,从装Anaconda开始. 版本相关:输入命令 cat /etc/redhat-release,我的版本是 CentOS Linu ...

  4. B/S,C/S架构的区别

    B/S架构:browser/server,采用的是浏览器服务器模式. C/S架构:client/server,采用的是客户端服务器模式. B/S架构,客户端是浏览器基本不需要维护,只需要维护升级服务器 ...

  5. 【React 7/100 】 虚拟DOM和Diff算法

    虚拟DOM和Diff算法 React更新视图的思想是:只要state变化就重新渲染视图 特点:思路非常清晰 问题:组件中只有一个DOM元素需要更新时,也得把整个组件的内容重新渲染吗? 不是这样的 理想 ...

  6. BloomFilter&python支持

    BloomFilter&python支持 BloomFilter 布隆过滤器是一种概率空间高效的数据结构.它与hashmap非常相似,用于检索一个元素是否在一个集合中.它在检索元素是否存在时, ...

  7. python-xss攻击和单例模式

    1.python中单例模式 class Foo: instance = None def __new__(cls, *args, **kwargs): if not Foo.instance: Foo ...

  8. 内置time模块和random模块

    #time模块#time模块中有三种时间表达方式#时间戳(timestamp):指从1970年1月1号0:0:0开始按秒计算的时间偏移量#元组形式的结构化时间(strut_time):含有9个元素(t ...

  9. Revolver Maps-3D地球仪网站定制

    这是个网站统计的小插件,大家可以看到那些国家,哪些城市对本网站进行了访问,很直观的一种表现方式. 这个小插件不需要你写任何代码,只需要去它官方网站定制你自己需要的代码.它的地址是:http://www ...

  10. Stylus-富有表现力的、动态的、健壮的CSS

    今天总结一下Stylus记一些常用的也是最基本的用法 一.  选择器 Stylus是基于缩进的这让我们可以更快捷的编写css比如 body { margin:; paddind:; font-size ...