Created
April 9, 2014 11:49
-
-
Save Tyralion/10259732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenoffice.app.ext; | |
| import android.util.Log; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.ObjectInputStream; | |
| import java.io.ObjectOutputStream; | |
| import java.io.OptionalDataException; | |
| import java.io.StreamCorruptedException; | |
| public class SettingStorage { | |
| private String file_name = "default.conf"; | |
| public SettingStorage(String file_name) { | |
| this.file_name = file_name; | |
| } | |
| public <T> T read() { | |
| File file = this.getFileStorage(); | |
| if (file == null || file.length() == 0) { | |
| return null; | |
| } | |
| FileInputStream fis = null; | |
| ObjectInputStream ois = null; | |
| T value = null; | |
| try { | |
| fis = new FileInputStream(file); | |
| ois = new ObjectInputStream(fis); | |
| value = (T) ois.readObject(); | |
| } catch (ClassNotFoundException ex) { | |
| Log.i("ClassNotFoundException", ex.getMessage()); | |
| } catch (OptionalDataException ex) { | |
| Log.i("OptionalDataException", ex.getMessage()); | |
| } catch (FileNotFoundException ex) { | |
| Log.i("FileNotFoundException", ex.getMessage()); | |
| } catch (StreamCorruptedException ex) { | |
| Log.i("StreamCorruptedException", ex.getMessage()); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| ex.printStackTrace();; | |
| } finally { | |
| if (ois != null) { | |
| try { | |
| ois.close(); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| } | |
| } | |
| if (fis != null) { | |
| try { | |
| fis.close(); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| } | |
| } | |
| } | |
| return value; | |
| } | |
| public <T> void write(T value) { | |
| File file = this.getFileStorage(); | |
| if (file == null) { | |
| return; | |
| } | |
| FileOutputStream fos = null; | |
| ObjectOutputStream obs = null; | |
| try { | |
| file.delete(); | |
| file.createNewFile(); | |
| fos = new FileOutputStream(file); | |
| obs = new ObjectOutputStream(fos); | |
| obs.writeObject(value); | |
| obs.flush(); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| } finally { | |
| if (obs != null) { | |
| try { | |
| obs.close(); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| } | |
| } | |
| if (fos != null) { | |
| try { | |
| fos.close(); | |
| } catch (IOException ex) { | |
| Log.i("IOException", ex.getMessage()); | |
| } | |
| } | |
| } | |
| } | |
| private File getFileStorage() { | |
| File file = new File(this.file_name); | |
| try { | |
| if (!file.exists()) { | |
| file.createNewFile(); | |
| } | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return file; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment