将可以序列化的对象通过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. C#获取当前路径7中方法

    //获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //获取 ...

  2. Codeforces 1262F Wrong Answer on test 233(组合数)

    E1:设dp[i][j],表示在第i个位置的当前新状态超过原状态j分的方案数是dp[i][j],那么对于这种情况直接进行转移即可,如果a[i]==b[i]显然k种选择都可以,不影响j,如果不一样,这个 ...

  3. honpeyhonepy

    2019.09.15 简历编辑功能: 2019.09.23 爬虫功能(智联招聘) 2.1 AI同步功能 2019.10.08 登录功能(包括普通用户登录.管理员.招聘人员) 2019.11.10 鉴权 ...

  4. linux:服务器代理squid安装配置

    国内上往外的网站太慢,配了个香港代理服务器.如下:当前环境: centos系统.香港服务器IP(假设:59.188.71.11)检查squid是否安装:[root@localhost ~]# rpm ...

  5. ECCV2014 Accepted paper

    今天早上看到小伙伴们说ECCV2014结果出来了, 自己于是赶紧看了下, 感觉ECCV2014显著性的文章和以往的不太一样. 1.Salient Montages from Unconstrained ...

  6. 使用Medusa美杜莎暴力破解SSH密码

    使用Medusa美杜莎暴力破解SSH密码 1.Medusa简介 Medusa(美杜莎)是一个速度快,支持大规模并行,模块化的爆力破解工具.可以同时对多个主机,用户或密码执行强力测试.Medusa和hy ...

  7. 【转】交换分区SWAP

    SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对 ...

  8. visual studio中配置opencv

    第1步附加包含目录:H:\software\programming\opencv\opencv\build\include 第2步附加库目录:H:\software\programming\openc ...

  9. vue2.0 之 生命周期

     一.vue1.x与vue2.x生命周期的变化区别及含义表(图表摘自网络)   二.vue2.x生命周期图和各阶段具体含义 beforecreated:el 和 data 并未初始化 created: ...

  10. git分支管理与tag的学习笔记

    git分支管理学习笔记:创建dev分支:git branch dev查看分支:git branch切换分支:git checkout dev创建并切换分支:git checkout dev -b zh ...