Created
May 16, 2014 10:28
-
-
Save zet4/fc236d564a3ade23901a to your computer and use it in GitHub Desktop.
A Java database class for a friend.
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.zetaworx.proboblyawesome.handler.database; | |
import java.io.File; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import cpw.mods.fml.common.FMLLog; | |
public class Database { | |
private boolean _isRAMOnly = true; //If true, means this database will exist only while application is running and won't save on shutdown. | |
private File _dbFile; //File for the database to work with. | |
private ArrayList<String> _memory; //Since we are working with String per line anyway... | |
public Database(String filelocation) throws IOException { | |
this(new File(filelocation)); | |
} | |
public Database(File file) throws IOException { | |
this(); | |
setFile(file); | |
} | |
public Database() { | |
_memory = new ArrayList<String>(); | |
} | |
public ArrayList<String> getData() { | |
return _memory; | |
} | |
public String getData(int index) { | |
return _memory.get(index); | |
} | |
public int addData(String data) throws Exception { | |
if (_memory.add(data)){ | |
return (_memory.size() - 1); | |
} else { | |
throw new Exception("Failed to add '" + data + "' to database memory."); | |
} | |
} | |
public String setData(int index, String data) { | |
return _memory.set(index, data); | |
} | |
public String remData(int index) { | |
return _memory.remove(index); | |
} | |
public void loadDatabase() throws IOException { | |
if (!_isRAMOnly) { | |
BufferedReader stream = new BufferedReader(new FileReader(_dbFile)); | |
String read; | |
while ((read = stream.readLine()) != null) | |
{ | |
_memory.add(read); | |
} | |
stream.close(); | |
} else { | |
//what'd you expect, there is nothing to load from silly. | |
} | |
} | |
public void saveDatabase() throws IOException { | |
if (!_isRAMOnly) { | |
if (_dbFile.exists()) { | |
File backup = new File(_dbFile.getAbsolutePath().concat(".bak")); | |
if (backup.exists()) backup.delete(); | |
_dbFile.renameTo(backup); | |
} | |
BufferedWriter stream = new BufferedWriter(new FileWriter(_dbFile)); | |
for (String s : _memory) { | |
stream.write(s.concat("\n")); | |
} | |
stream.close(); | |
} else { | |
//nor is there a place to save it to silly... | |
} | |
} | |
public void resetDatabase(boolean hard) throws IOException { | |
if (hard && !_isRAMOnly) { | |
//HARDCORE RESET, nah, but really deletes db and db.bak file's. | |
_dbFile.delete(); | |
(new File(_dbFile.getAbsolutePath().concat(".bak"))).delete(); | |
} | |
_memory.clear(); | |
if (!hard) loadDatabase(); //We still have somewhere to load from, right?... | |
} | |
public void setFile(File file) throws IOException { | |
FMLLog.info("%s\n", file.getAbsolutePath()); | |
_dbFile = file; | |
if (!_dbFile.exists()) _dbFile.createNewFile(); | |
_isRAMOnly = false; | |
} | |
public File getFile() { | |
return _dbFile; | |
} | |
public String getFileLocation(boolean canonical) { | |
return _dbFile.getAbsolutePath(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment