Advertisement
Guest User

DataFile.java

a guest
Nov 10th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. package me.henry.dataapi;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8.  
  9. /**
  10. * <h1><b>DataFile</b></h1>
  11. * The DataFile class lets you store and retrieve data easily from files outside the program.
  12. *
  13. * @author Skionz
  14. */
  15. public class DataFile {
  16. private String path;
  17. private NewProperties prop;
  18. /**
  19. * Adds a row with a key and value to the data file
  20. *
  21. * @param path Sets the path to the file
  22. * @param extension Sets extension of the file. Don't add a period to the extension
  23. */
  24. public DataFile(String path, String extension) {
  25. try {
  26. this.path = path + "." + extension;
  27. File file = new File(this.path);
  28. this.prop = new NewProperties();
  29. if(file.exists()) {
  30. FileReader reader = new FileReader(this.path);
  31. prop.load(reader);
  32. for(Object set : this.prop.keySet()) {
  33. String val = this.prop.getProperty((String) set);
  34. this.prop.setProperty((String) set, val);
  35. }
  36. reader.close();
  37. }
  38. FileOutputStream stream = new FileOutputStream(this.path);
  39. this.prop.store(stream, null);
  40. stream.close();
  41. } catch(Exception e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. /**
  46. * Adds a row with a key and value to the data file
  47. *
  48. * @param key Sets the key
  49. * @param value Sets the value of the key
  50. */
  51. public void set(String key, String value) {
  52. try {
  53. FileReader reader = new FileReader(this.path);
  54. this.prop = new NewProperties();
  55. this.prop.load(reader);
  56. FileOutputStream stream = new FileOutputStream(this.path);
  57. for(Object set : this.prop.keySet()) {
  58. String val = this.prop.getProperty(set.toString());
  59. this.prop.setProperty(set.toString(), val);
  60. }
  61. reader.close();
  62. this.prop.setProperty(key, value);
  63. this.prop.store(stream, null);
  64. stream.close();
  65. } catch(Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * Adds a row with a key and value to the data file
  71. *
  72. * @param key Sets the key
  73. * @param value Sets the value of the key
  74. */
  75. public void set(String key, DataList value) {
  76. try {
  77. FileReader reader = new FileReader(this.path);
  78. this.prop = new NewProperties();
  79. this.prop.load(reader);
  80. FileOutputStream stream = new FileOutputStream(this.path);
  81. for(Object set : this.prop.keySet()) {
  82. String val = this.prop.getProperty(set.toString());
  83. this.prop.setProperty(set.toString(), val);
  84. }
  85. reader.close();
  86. this.prop.setProperty(key, value.toString());
  87. this.prop.store(stream, null);
  88. stream.close();
  89. } catch(Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * Removes a row from the data file
  95. *
  96. * @param key The key of the row to remove
  97. *
  98. */
  99. public void remove(String key) {
  100. try {
  101. FileReader reader = new FileReader(path);
  102. this.prop = new NewProperties();
  103. this.prop.load(reader);
  104. FileOutputStream stream = new FileOutputStream(path);
  105. for(Object set : this.prop.keySet()) {
  106. String val = this.prop.getProperty(set.toString());
  107. this.prop.setProperty(set.toString(), val);
  108. }
  109. reader.close();
  110. this.prop.remove(key);
  111. this.prop.store(stream, null);
  112. stream.close();
  113. } catch(Exception e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. /**
  118. * Gets the value of the specified key
  119. *
  120. * @param key Sets the key
  121. * @return Returns the value of the specified key
  122. */
  123. public Object get(String key) {
  124. try {
  125. Object value = this.prop.get(key);
  126. return value;
  127. } catch(Exception e) {
  128. e.printStackTrace();
  129. }
  130. return null;
  131. }
  132. /**
  133. * Gets a list for the specified key
  134. *
  135. * @param key Sets the key
  136. * @return Returns a DataFileList
  137. */
  138. public DataList getList(String key) {
  139. try {
  140. String stringValue = this.prop.getProperty(key.toString());
  141. DataList value = this.stringToList(stringValue);
  142. return value;
  143. } catch(Exception e) {
  144. e.printStackTrace();
  145. }
  146. return null;
  147. }
  148. /**
  149. * Checks if the data file contains the specified key
  150. *
  151. * @param key The key to check for
  152. *
  153. */
  154. public boolean contains(String key) {
  155. return prop.containsKey(key);
  156. }
  157. /**
  158. *
  159. * @return Returns a Set that includes every key and its value
  160. */
  161. public Set<Entry<Object, Object>> entrySet() {
  162. try {
  163. return prop.entrySet();
  164. } catch(Exception e) {
  165. e.printStackTrace();
  166. }
  167. return null;
  168. }
  169. /**
  170. * Clears the entire data file
  171. */
  172. public void clear() {
  173. try {
  174. FileOutputStream stream = new FileOutputStream(path);
  175. prop.clear();
  176. prop.store(stream, null);
  177. stream.close();
  178. } catch(Exception e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. /**
  183. * @return Returns the size of the data file
  184. */
  185. public int size() {
  186. return prop.size();
  187. }
  188. /**
  189. * @return Returns the size of the data file
  190. */
  191. public int length() {
  192. return prop.size();
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. private DataList stringToList(String string) {
  202. DataList list = new DataList();
  203. String[] split = string.split(",");
  204. for(int count = 0; count <= split.length - 1; count++) {
  205. list.add(split[count]);
  206. }
  207. return list;
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement