将可以序列化的对象通过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. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...

  2. The order of a Tree

    The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...

  3. Vue切换页面时中断axios请求

    一.概述 在Vue单页面开发过程中,遇到这样的情况,当我切换页面时,由于上一页面请求执行时间长,切换到该页面时,还未执行完,这时那个请求仍会继续执行直到请求结束,此时将会影响页面性能,并且可能对现在页 ...

  4. PHPstorm快捷键介绍总结

    如下所示: Eclipse快捷键 Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt ...

  5. js面向过程-拖拽

    1.步骤分析: 1.1 获取id 1.2 当鼠标点击时执行的js 1.3当鼠标移动时执行的js 1.4当鼠标放开时执行的js 2.代码实现 <!DOCTYPE html> <html ...

  6. IDEA tomcat热部署方法

    项目开发过程中,我们一般希望在修改完代码之后不重启项目即可提现出修改的结果,那么热部署项目就显得十分必要了.在idea中将项目热部署至tomcat中的方法如下: 首先打开tomcat配置界面,在ser ...

  7. python的java胶水(jpype1)

    1.直接使用pip安装jpype1 命令 pip install jpype1 但是,很不幸,提示报错,缺少VC++组件. 2.使用其他方法安装 在  https://www.lfd.uci.edu/ ...

  8. Linux之文件属性、权限

    Linux中的3种身份:1. owner(文件所有者) 2. group(用户组) 3. others(其他) Linux中的3中权限:1. r(可读) 2. w(可写) 3. x(可执行) * 所有 ...

  9. 为什么我选择用 flutter

    1. flutter 生成的是机器代码,他既不是 hybrid 也不是transpiler,  因此有很高的执行效率. 2. declarative ui,这不是什么新的概念,在 react vue ...

  10. angular,,以及深度拷贝问题;JSON.parse,JSON.stringify灵活运用

    问题: $scope.list = [];$scope.listTree = {};$scope.dataTree = []; //获取listTree的数据$scope.getList = func ...