Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Die statische Klasse Dini stellt Funktionen für eine Dateiverwaltung über Identifikations Keys zur verfügung
- *
- * @author Felix Hageneier
- * @version 0.1
- */
- import java.io.*;
- public class dini
- {
- private static File rootdirectoryfile = new File(System.getProperty("user.dir"));
- private static String rootdirectory = rootdirectoryfile.toString().concat("\\");
- public dini()
- {
- }
- /**
- * Diese Methode überprüft, ob eine Datei existiert
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @return boolean true - Wenn Datei existiert, false - Wenn Datei nicht existiert
- */
- public static boolean dini_Create(String filepath) throws IOException
- {
- String pfilepath = rootdirectory.concat(filepath);
- File newfile = new File(pfilepath);
- File parent = new File(newfile.getParent());
- parent.mkdirs();
- File namefile = new File(rootdirectory.concat(newfile.getName()));
- return newfile.createNewFile();
- }
- /**
- * Diese Methode löscht die angegebene Datei, wenn diese existiert
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @return boolean true - Wenn die Datei gelöscht wurde, false - Wenn Datei nicht existiert oder gelöscht wurde
- */
- public static boolean dini_Remove(String filepath) throws IOException
- {
- String pfilepath = rootdirectory.concat(filepath);
- File newfile = new File(pfilepath);
- return newfile.delete();
- }
- /**
- * Diese Methode erstellt eine Datei im Zielverzeichnis
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @return boolean true - Wenn erfolgreich, false - Wenn nicht erfolgreich
- */
- public static boolean dini_Exists(String filepath) throws IOException
- {
- String pfilepath = rootdirectory.concat(filepath);
- File newfile = new File(pfilepath);
- return newfile.exists();
- }
- /**
- * Diese Methode überprüft, ob ein Key existiert
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param key Der Key
- * @return boolean true - Wenn der Key existiert, false - Wenn der Key nicht existiert
- */
- public static boolean dini_Isset(String filepath,String key) throws IOException
- {
- if(dini_String(filepath,key) != null) return true;
- return false;
- }
- /**
- * Diese Methode löscht einen Key aus einer File
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param key Der zu löschende Key
- * @return boolean true - Wenn der Key gelöscht wurde, false - Wenn der Key nicht existiert oder gelöscht wurde
- */
- public static boolean dini_Unset(String filepath,String key) throws IOException
- {
- if(dini_Exists(filepath) == false) return false;
- if(dini_Isset(filepath,key) == false) return false;
- File tmp = File.createTempFile("tmp",".tmp");
- try(BufferedReader br = new BufferedReader(new FileReader(filepath)))
- {
- try(BufferedWriter bw = new BufferedWriter(new FileWriter(tmp.toString())))
- {
- String ausw;
- while((ausw = br.readLine()) != null)
- {
- if(ausw.substring(0,ausw.indexOf("=")).equals(key)) continue;
- bw.write(ausw);
- bw.newLine();
- }
- }
- }
- boolean removed = dini_Remove(filepath);
- boolean renamed = tmp.renameTo(new File(filepath));
- return true;
- }
- /**
- * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen String
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Value
- * @param Value Zu speichernder String
- * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
- */
- public static boolean dini_StringSet(String filepath, String key, String value) throws IOException
- {
- if(key.length() <= 0 || value.length() <= 0) return false;
- String pfilepath = rootdirectory.concat(filepath);
- File pfile = new File(pfilepath);
- //dini_Remove(filepath.concat(".part"));
- if(dini_Exists(filepath) == false) return false;
- // Checkt ob der Key schon existiert
- boolean foundkeybool = false;
- File tmp = null;
- try(BufferedReader br = new BufferedReader(new FileReader(pfilepath)))
- {
- String ausw;
- while((ausw = br.readLine()) != null)
- {
- String foundkey = ausw.substring(0,ausw.indexOf("="));
- System.out.println(foundkey);
- if(foundkey.equals(key))
- {
- foundkeybool = true;
- System.out.println(foundkeybool);
- //Key exists and content has to be overwritten
- String newline = key.concat("=").concat(value);
- tmp = File.createTempFile("tmp",".tmp");
- String currentLine;
- try(BufferedWriter bw = new BufferedWriter(new FileWriter(tmp.toString())))
- {
- try(BufferedReader br2 = new BufferedReader(new FileReader(pfilepath)))
- {
- while((currentLine = br2.readLine()) != null)
- {
- //trim newline when comparing with lineToRemove
- String trimmedLine = currentLine.trim();
- System.out.println(trimmedLine);
- if(trimmedLine.equals(ausw))
- {
- System.out.println("Austauschen: "+newline);
- bw.write(newline);
- }
- else
- {
- bw.write(currentLine);
- System.out.println("Lassen: "+currentLine);
- }
- bw.newLine();
- }
- }
- }
- break;
- }
- }
- }
- if(foundkeybool == true)
- {
- tmp.setWritable(true);
- pfile.setWritable(true);
- boolean removed = dini_Remove(filepath);
- boolean renamed = tmp.renameTo(pfile);
- }
- else //(foundkeybool == false) if key does not exists we can create a new one
- {
- try(BufferedWriter bw = new BufferedWriter(new FileWriter(pfilepath,true)))
- {
- bw.write(key.concat("=").concat(value));
- bw.newLine();
- bw.close();
- }
- }
- return true;
- }
- /**
- * Diese Methode liest aus einer Datei den String Wert hinter einem Key aus
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Der auszulesende Key
- * @return Der String welcher unter dem Key gespeichert wurde bzw. null wenn der Key nicht existiert.
- */
- public static String dini_String(String filepath, String key) throws IOException
- {
- if(key.length() <= 0) return null;
- String pfilepath = rootdirectory.concat(filepath);
- File pfile = new File(pfilepath);
- //dini_Remove(filepath.concat(".part"));
- if(dini_Exists(filepath) == false) return null;
- boolean foundkeybool = false;
- String returnstring = null;
- try(BufferedReader br = new BufferedReader(new FileReader(pfilepath)))
- {
- String line = null;
- while((line = br.readLine()) != null)
- {
- String foundkey = line.substring(0,line.indexOf("="));
- if(foundkey.equals(key))
- {
- foundkeybool = true;
- returnstring = line.substring(line.indexOf("=")+1,line.length());
- }
- }
- }
- if(foundkeybool == true)
- {
- return returnstring;
- }
- else
- {
- return null;
- }
- }
- /**
- * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Integer
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Value
- * @param Value Zu speichernder Integer
- * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
- */
- public static boolean dini_IntSet(String filepath, String key,int value) throws IOException
- {
- Integer valueint = new Integer(value);
- return dini_StringSet(filepath,key,valueint.toString());
- }
- /**
- * Diese Methode liefert einen Integer Wert, welcher unter einem Key gespeichert ist
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Integer
- * @return Den Integer Wert bzw. Integer.MIN_VALUE bei einem Fehler
- */
- public static int dini_Int(String filepath, String key) throws IOException
- {
- String intString = dini_String(filepath,key);
- int val;
- try
- {
- val = Integer.parseInt(intString);
- return val;
- }
- catch (NumberFormatException nfe)
- {
- return Integer.MIN_VALUE;
- }
- }
- /**
- * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Boolean
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Value
- * @param Value Zu speichernder Boolean
- * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
- */
- public static boolean dini_BoolSet(String filepath, String key,boolean value) throws IOException
- {
- Boolean valuebool = new Boolean(value);
- return dini_StringSet(filepath,key,valuebool.toString());
- }
- /**
- * Diese Methode liefert einen Boolean Wert, welcher unter einem Key gespeichert ist
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Boolean
- * @return Den Boolean Wert
- */
- public static boolean dini_Bool(String filepath, String key) throws IOException
- {
- String boolString = dini_String(filepath,key);
- boolean val;
- if("true".equals(boolString) || "false".equals(boolString))
- {
- val = Boolean.parseBoolean(boolString);
- }
- else
- {
- val = false;
- }
- return val;
- }
- /**
- * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Float
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Value
- * @param Value Zu speichernder Float
- * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
- */
- public static boolean dini_FloatSet(String filepath, String key,float value) throws IOException
- {
- Float valuefloat = new Float(value);
- return dini_StringSet(filepath,key,valuefloat.toString());
- }
- /**
- * Diese Methode liefert einen Float Wert, welcher unter einem Key gespeichert ist
- *
- * @param filepath Der Dateipath, ausgehend vom Programm Root Path
- * @param Key Die Key Flag für den Float
- * @return Den Float Wert oder den MIN_VALUE bei einem Fehler
- */
- public static float dini_Float(String filepath, String key) throws IOException
- {
- String floatString = dini_String(filepath,key);
- float val;
- try
- {
- val = Float.parseFloat(floatString);
- return val;
- }
- catch (NumberFormatException nfe)
- {
- return Float.MIN_VALUE;
- }
- }
- }
Add Comment
Please, Sign In to add comment