2018-01-31 15:03:53 +00:00
|
|
|
package me.konloch.kontainer.io;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from the disk, optional caching
|
2018-01-31 15:41:24 +00:00
|
|
|
*
|
2018-01-31 15:03:53 +00:00
|
|
|
* @author Konloch
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class DiskReader {
|
|
|
|
|
2018-01-31 15:41:24 +00:00
|
|
|
public static Random random = new Random();
|
|
|
|
public static HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from file, allows caching
|
|
|
|
*/
|
|
|
|
public synchronized static ArrayList<String> loadArrayList(String fileName,
|
|
|
|
boolean cache) {
|
|
|
|
ArrayList<String> array = new ArrayList<String>();
|
|
|
|
if (!map.containsKey(fileName)) {
|
|
|
|
try {
|
|
|
|
File file = new File(fileName);
|
|
|
|
if (!file.exists()) // doesnt exist, return empty
|
|
|
|
return array;
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
|
String add;
|
|
|
|
|
|
|
|
while ((add = reader.readLine()) != null)
|
|
|
|
array.add(add);
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
if (cache)
|
|
|
|
map.put(fileName, array);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
array = map.get(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load from file
|
|
|
|
*/
|
|
|
|
public synchronized static String loadAsString(String fileName)
|
|
|
|
throws Exception {
|
|
|
|
String s = "";
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(new File(
|
|
|
|
fileName)));
|
|
|
|
String add;
|
|
|
|
|
|
|
|
while ((add = reader.readLine()) != null)
|
|
|
|
s += add + System.getProperty("line.separator");
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to load a string via line number lineNumber = -1 means random.
|
|
|
|
*/
|
|
|
|
public static String loadString(String fileName, int lineNumber,
|
|
|
|
boolean cache) throws Exception {
|
|
|
|
|
|
|
|
ArrayList<String> array;
|
|
|
|
if (!map.containsKey(fileName)) {
|
|
|
|
array = new ArrayList<String>();
|
|
|
|
File file = new File(fileName);
|
|
|
|
|
|
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
|
String add;
|
|
|
|
|
|
|
|
while ((add = reader.readLine()) != null)
|
|
|
|
array.add(add);
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
if (cache)
|
|
|
|
map.put(fileName, array);
|
|
|
|
} else {
|
|
|
|
array = map.get(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineNumber == -1) {
|
|
|
|
int size = array.size();
|
|
|
|
return array.get(random.nextInt(size));
|
|
|
|
} else
|
|
|
|
return array.get(lineNumber);
|
|
|
|
}
|
2018-01-31 15:03:53 +00:00
|
|
|
|
|
|
|
}
|