将可以序列化的对象通过base64编码后进行保存
但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
sd卡文件读写工具类
这个类还是有可以改进之处的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
从asserts目录下读取文件的工具类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); }