当前位置: 代码迷 >> Android >> android 将一些信息保留到本地,以后更新版本或者替换之后,数据在新安装的项目中还能使用
  详细解决方案

android 将一些信息保留到本地,以后更新版本或者替换之后,数据在新安装的项目中还能使用

热度:76   发布时间:2016-05-01 20:20:35.0
android 将一些信息保存到本地,以后更新版本或者替换之后,数据在新安装的项目中还能使用
HongxinLocalSaveUtil



public class HongxinLocalSaveUtil{



    private static final String SAVE_URL = "/youban/radio/cache/hongxindata/";



    private static final String FILENAME = "record.cfg";



    private static HongxinLocalSaveUtilinstance = null;



    private static String SDPATH;



    public static HongxinLocalSaveUtilgetInstance() {

       if (instance == null) {

           instance = new HongxinLocalSaveUtil();

       }

       return instance;

    }



    private HongxinLocalSaveUtil() {

       SDPATH = android.os.Environment.getExternalStorageDirectory()+ "";

    }



    /**

     * 是否能创建和写入文件

     */

    public boolean isCanCreateAndWriteFile() {

       if (Environment.getExternalStorageState().equals(

              Environment.MEDIA_MOUNTED)) {

           return true;

       }

       return false;

    }



    // 创建目录

    public File createDir(String fileName) throws IOException {

       File dir = new File(SDPATH + fileName);

       dir.mkdirs();

       return dir;

    }



    // 创建文件

    public File createFile(String fileName) throws IOException {

       File file = new File(SDPATH + fileName);

       file.createNewFile();

       return file;

    }



    public void saveRecord(List<MusicData> musicDatas) {

     

       if (Environment.getExternalStorageState().equals(

              Environment.MEDIA_MOUNTED)) {



           FileOutputStream outStream = null;

           DataOutputStream dos = null;

           try {

               if (!isExist()) {

                  File file = new File(SDPATH + SAVE_URL + FILENAME);

                  if (file != null) {

                     file.delete();

                  }

              }



              createDir(SAVE_URL);

              File file = createFile(SAVE_URL + FILENAME);



              outStream = new FileOutputStream(file);

               dos = newDataOutputStream(outStream);

              if (musicDatas != null) {

                  int size = musicDatas.size();

                  dos.writeInt(size);

                  for (int i = 0 ; i < size ; i++) {

                     MusicData musicData = musicDatas.get(i);

                     if (musicData == null) {

                         continue;

                     }

                     dos.writeUTF(musicData.getImageUrl());

                     dos.writeUTF(musicData.getMp3Long());

                     dos.writeUTF(musicData.getMusicName());

                     dos.writeUTF(musicData.getPlayLink());

                     dos.writeInt(musicData.getID());

                     dos.writeUTF(musicData.getLyrics());

                  }

              }else {

                  dos.writeInt(0);

              }

              dos.flush();

           } catch (FileNotFoundException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

           } catch (IOException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

           } finally {

              if (dos != null) {

                  try {

                     dos.close();

                  } catch (IOException e) {

                     // TODO Auto-generatedcatch block

                     e.printStackTrace();

                  }

              }

              if (outStream != null) {

                  try {

                     outStream.close();

                  } catch (Exception e) {

                  }

              }

           }

       }

    }



    // 判断文件是否存在

    public boolean isExist() {

       File file = new File(SDPATH + SAVE_URL + FILENAME);

       return file.exists();

    }



    public List<MusicData> readData() {

       /* sdcard可读状态 */

       if (android.os.Environment.getExternalStorageState().equals(

              android.os.Environment.MEDIA_MOUNTED)) {



           if (!isExist()) {

              return null;

           }

         

           FileInputStream fis = null;

           DataInputStream dis = null;

           try {

              File file = new File(SDPATH + SAVE_URL + FILENAME);

              fis = new FileInputStream(file);

              dis = new DataInputStream(fis);



              int size = dis.readInt();

            

              if (size == 0) {

                  return null;

              }

            

              List<MusicData> musicDatas = newArrayList<MusicData>();

            

              for (int i = 0 ; i < size ; i++) {

                  MusicData musicData = new MusicData();

                  musicData.setImageUrl(dis.readUTF());

                  musicData.setMp3Long(dis.readUTF());

                  musicData.setMusicName(dis.readUTF());

                  musicData.setPlayLink(dis.readUTF());

                  musicData.setID(dis.readInt());

                  musicData.setLyrics(dis.readUTF());

                  musicDatas.add(musicData);

              }

            

              return musicDatas;

           } catch (Exception e) {

           } finally {

              if (dis != null) {

                  try {

                     dis.close();

                  } catch (IOException e) {

                     // TODO Auto-generatedcatch block

                     e.printStackTrace();

                  }

              }

              if (fis != null) {

                  try {

                     fis.close();

                  } catch (Exception e) {

                  }

              }

           }

       }

       return null;

    }

}



需要导入权限

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  相关解决方案