zoukankan      html  css  js  c++  java
  • android 文件读写工具类

    将可以序列化的对象通过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目录文件读写
  • 相关阅读:
    列表、元组、字典、集合类型及其内置方法
    Python数字类型及字符串类型的内置方法 ##
    Python之流程控制
    前端混合
    数据库
    oracle 11g 安装过程
    SQLAlchemy
    pipreqs快速生成python项目所需的依赖
    llinux基本操作
    linux简介
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3422966.html
Copyright © 2011-2022 走看看