Guest User

Saver class

a guest
Jul 3rd, 2016
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import android.content.Context;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.math.BigDecimal;
  6. import java.math.BigInteger;
  7.  
  8. public class Saver {
  9.  
  10.  
  11. static FileOutputStream fos;
  12.  
  13. static FileInputStream fis;
  14.  
  15.  
  16. public static void save(String filename, String data, Context c){
  17.  
  18.  
  19.  
  20.  
  21. try {
  22.  
  23. fos = c.openFileOutput(filename, Context.MODE_PRIVATE);
  24.  
  25. fos.write(data.getBytes());
  26. fos.close();
  27.  
  28. } catch (Exception e) {
  29.  
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33.  
  34.  
  35.  
  36. }
  37.  
  38.  
  39.  
  40. String file;
  41. public String getFile(){return file;}
  42.  
  43. public void setFile(String loc){
  44. file = loc;
  45. }
  46.  
  47. String result;
  48. private static String mainLoad(String fn, Context c){
  49. String collected = null;
  50. try{
  51.  
  52. fis = c.openFileInput(fn);
  53. byte[] dataArray = new byte[fis.available()];
  54.  
  55. while(fis.read(dataArray) != -1){
  56. collected = new String(dataArray);
  57. }
  58.  
  59.  
  60. }catch(Exception e){
  61. e.printStackTrace();
  62. return null;
  63.  
  64. }finally{
  65. try {
  66. fis.close();
  67.  
  68. return collected;
  69. } catch (Exception e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. return null;
  73. }
  74. }
  75.  
  76.  
  77. }
  78.  
  79. public static int loadInt(String fn, Context c){
  80. if(mainLoad(fn,c) == null) return 0;
  81. else return Integer.parseInt(mainLoad(fn,c));
  82. }
  83. public static double loadDouble(String fn, Context c){
  84. if(mainLoad(fn,c) == null) return 0;
  85. else return Double.parseDouble(mainLoad(fn,c));
  86. }
  87.  
  88. public static float loadFloat(String fn, Context c){
  89. return Float.parseFloat(mainLoad(fn,c));
  90. }
  91.  
  92. public static String loadString(String fn, Context c){
  93. return mainLoad(fn, c);
  94. }
  95.  
  96. public static Boolean loadBoolean(String fn, Context c){
  97. if(mainLoad(fn,c) == null) return false;
  98. else return Boolean.parseBoolean(mainLoad(fn,c));
  99. }
  100.  
  101. public static BigInteger loadBigInteger(String fn, Context c){
  102.  
  103. return new BigInteger(mainLoad(fn,c));
  104. }
  105.  
  106. public static BigDecimal loadBigDecimal(String fn, Context c){
  107. return new BigDecimal(mainLoad(fn,c));
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment