Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.henry.dataapi;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.util.Map.Entry;
- import java.util.Set;
- /**
- * <h1><b>DataFile</b></h1>
- * The DataFile class lets you store and retrieve data easily from files outside the program.
- *
- * @author Skionz
- */
- public class DataFile {
- private String path;
- private NewProperties prop;
- /**
- * Adds a row with a key and value to the data file
- *
- * @param path Sets the path to the file
- * @param extension Sets extension of the file. Don't add a period to the extension
- */
- public DataFile(String path, String extension) {
- try {
- this.path = path + "." + extension;
- File file = new File(this.path);
- this.prop = new NewProperties();
- if(file.exists()) {
- FileReader reader = new FileReader(this.path);
- prop.load(reader);
- for(Object set : this.prop.keySet()) {
- String val = this.prop.getProperty((String) set);
- this.prop.setProperty((String) set, val);
- }
- reader.close();
- }
- FileOutputStream stream = new FileOutputStream(this.path);
- this.prop.store(stream, null);
- stream.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Adds a row with a key and value to the data file
- *
- * @param key Sets the key
- * @param value Sets the value of the key
- */
- public void set(String key, String value) {
- try {
- FileReader reader = new FileReader(this.path);
- this.prop = new NewProperties();
- this.prop.load(reader);
- FileOutputStream stream = new FileOutputStream(this.path);
- for(Object set : this.prop.keySet()) {
- String val = this.prop.getProperty(set.toString());
- this.prop.setProperty(set.toString(), val);
- }
- reader.close();
- this.prop.setProperty(key, value);
- this.prop.store(stream, null);
- stream.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Adds a row with a key and value to the data file
- *
- * @param key Sets the key
- * @param value Sets the value of the key
- */
- public void set(String key, DataList value) {
- try {
- FileReader reader = new FileReader(this.path);
- this.prop = new NewProperties();
- this.prop.load(reader);
- FileOutputStream stream = new FileOutputStream(this.path);
- for(Object set : this.prop.keySet()) {
- String val = this.prop.getProperty(set.toString());
- this.prop.setProperty(set.toString(), val);
- }
- reader.close();
- this.prop.setProperty(key, value.toString());
- this.prop.store(stream, null);
- stream.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Removes a row from the data file
- *
- * @param key The key of the row to remove
- *
- */
- public void remove(String key) {
- try {
- FileReader reader = new FileReader(path);
- this.prop = new NewProperties();
- this.prop.load(reader);
- FileOutputStream stream = new FileOutputStream(path);
- for(Object set : this.prop.keySet()) {
- String val = this.prop.getProperty(set.toString());
- this.prop.setProperty(set.toString(), val);
- }
- reader.close();
- this.prop.remove(key);
- this.prop.store(stream, null);
- stream.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Gets the value of the specified key
- *
- * @param key Sets the key
- * @return Returns the value of the specified key
- */
- public Object get(String key) {
- try {
- Object value = this.prop.get(key);
- return value;
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Gets a list for the specified key
- *
- * @param key Sets the key
- * @return Returns a DataFileList
- */
- public DataList getList(String key) {
- try {
- String stringValue = this.prop.getProperty(key.toString());
- DataList value = this.stringToList(stringValue);
- return value;
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Checks if the data file contains the specified key
- *
- * @param key The key to check for
- *
- */
- public boolean contains(String key) {
- return prop.containsKey(key);
- }
- /**
- *
- * @return Returns a Set that includes every key and its value
- */
- public Set<Entry<Object, Object>> entrySet() {
- try {
- return prop.entrySet();
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Clears the entire data file
- */
- public void clear() {
- try {
- FileOutputStream stream = new FileOutputStream(path);
- prop.clear();
- prop.store(stream, null);
- stream.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @return Returns the size of the data file
- */
- public int size() {
- return prop.size();
- }
- /**
- * @return Returns the size of the data file
- */
- public int length() {
- return prop.size();
- }
- private DataList stringToList(String string) {
- DataList list = new DataList();
- String[] split = string.split(",");
- for(int count = 0; count <= split.length - 1; count++) {
- list.add(split[count]);
- }
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement