Lechivre

Untitled

Jul 25th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.75 KB | None | 0 0
  1.  /**
  2.  * Die statische Klasse Dini stellt Funktionen für eine Dateiverwaltung über Identifikations Keys zur verfügung
  3.  *
  4.  * @author Felix Hageneier
  5.  * @version 0.1
  6.  */
  7.  
  8. import java.io.*;
  9.  
  10. public class dini
  11. {
  12.     private static File rootdirectoryfile = new File(System.getProperty("user.dir"));
  13.     private static String rootdirectory = rootdirectoryfile.toString().concat("\\");
  14.    
  15.     public dini()
  16.     {
  17.     }
  18.  
  19.     /**
  20.      * Diese Methode erstellt eine Datei im Zielverzeichnis
  21.      *
  22.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  23.      * @return boolean true - Wenn erfolgreich, false - Wenn nicht erfolgreich
  24.      */
  25.     public static boolean dini_Create(String filepath) throws IOException
  26.     {
  27.         String pfilepath = rootdirectory.concat(filepath);
  28.         File newfile = new File(pfilepath);
  29.         File parent = new File(newfile.getParent());
  30.         parent.mkdirs();
  31.         File namefile = new File(rootdirectory.concat(newfile.getName()));
  32.         return newfile.createNewFile();
  33.     }
  34.    
  35.     /**
  36.      * Diese Methode löscht die angegebene Datei, wenn diese existiert
  37.      *
  38.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  39.      * @return boolean true - Wenn die Datei gelöscht wurde, false - Wenn Datei nicht existiert oder gelöscht wurde
  40.      */
  41.     public static boolean dini_Remove(String filepath) throws IOException
  42.     {
  43.         String pfilepath = rootdirectory.concat(filepath);
  44.         File newfile = new File(pfilepath);
  45.         return newfile.delete();
  46.     }
  47.    
  48.     /**
  49.      * Diese Methode überprüft, ob eine Datei existiert
  50.      *
  51.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  52.      * @return boolean true - Wenn Datei existiert, false - Wenn Datei nicht existiert
  53.      */
  54.     public static boolean dini_Exists(String filepath) throws IOException
  55.     {
  56.         String pfilepath = rootdirectory.concat(filepath);
  57.         File newfile = new File(pfilepath);
  58.         return newfile.exists();
  59.     }
  60.    
  61.      /**
  62.      * Diese Methode überprüft, ob ein Key existiert
  63.      *
  64.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  65.      * @param key Der Key
  66.      * @return boolean true - Wenn der Key existiert, false - Wenn der Key nicht existiert
  67.      */
  68.     public static boolean dini_Isset(String filepath,String key) throws IOException
  69.     {
  70.         if(dini_String(filepath,key) != null) return true;
  71.         return false;
  72.     }
  73.    
  74.      /**
  75.      * Diese Methode löscht einen Key aus einer File
  76.      *
  77.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  78.      * @param key Der zu löschende Key
  79.      * @return boolean true - Wenn der Key gelöscht wurde, false - Wenn der Key nicht existiert oder gelöscht wurde
  80.      */
  81.     public static boolean dini_Unset(String filepath,String key) throws IOException
  82.     {
  83.         if(dini_Exists(filepath) == false) return false;
  84.         if(dini_Isset(filepath,key) == false) return false;
  85.        
  86.         File tmp = File.createTempFile("tmp",".tmp");
  87.         try(BufferedReader br = new BufferedReader(new FileReader(filepath)))
  88.         {          
  89.             try(BufferedWriter bw = new BufferedWriter(new FileWriter(tmp.toString())))
  90.             {
  91.                 String ausw;
  92.                
  93.                 while((ausw = br.readLine()) != null)
  94.                 {
  95.                     if(ausw.substring(0,ausw.indexOf("=")).equals(key)) continue;
  96.                     bw.write(ausw);
  97.                     bw.newLine();
  98.                 }
  99.             }
  100.         }
  101.        
  102.         boolean removed = dini_Remove(filepath);
  103.         boolean renamed = tmp.renameTo(new File(filepath));
  104.        
  105.         return true;
  106.     }
  107.    
  108.     /**
  109.      * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen String
  110.      *
  111.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  112.      * @param Key Die Key Flag für den Value
  113.      * @param Value Zu speichernder String
  114.      * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
  115.      */
  116.     public static boolean dini_StringSet(String filepath, String key, String value) throws IOException
  117.     {
  118.         if(key.length() <= 0 || value.length() <= 0) return false;
  119.  
  120.         String pfilepath = rootdirectory.concat(filepath);
  121.         File pfile = new File(pfilepath);
  122.         //dini_Remove(filepath.concat(".part"));
  123.        
  124.         if(dini_Exists(filepath) == false) return false;
  125.        
  126.         // Checkt ob der Key schon existiert
  127.        
  128.         boolean foundkeybool = false;
  129.         File tmp = null;
  130.        
  131.         try(BufferedReader br = new BufferedReader(new FileReader(pfilepath)))
  132.         {          
  133.             String ausw;
  134.            
  135.             while((ausw = br.readLine()) != null)
  136.             {
  137.                String foundkey = ausw.substring(0,ausw.indexOf("="));
  138.                System.out.println(foundkey);
  139.                if(foundkey.equals(key))
  140.                {
  141.                    foundkeybool = true;
  142.                    System.out.println(foundkeybool);
  143.                    
  144.                    //Key exists and content has to be overwritten
  145.                    
  146.                    String newline = key.concat("=").concat(value);
  147.                    
  148.                    
  149.                    tmp = File.createTempFile("tmp",".tmp");
  150.                    String currentLine;
  151.                    
  152.                    try(BufferedWriter bw = new BufferedWriter(new FileWriter(tmp.toString())))
  153.                    {
  154.                        try(BufferedReader br2 = new BufferedReader(new FileReader(pfilepath)))                
  155.                        {
  156.                            while((currentLine = br2.readLine()) != null)
  157.                            {
  158.                                 //trim newline when comparing with lineToRemove
  159.                                 String trimmedLine = currentLine.trim();
  160.                                 System.out.println(trimmedLine);
  161.                                 if(trimmedLine.equals(ausw))
  162.                                 {
  163.                                     System.out.println("Austauschen: "+newline);
  164.                                     bw.write(newline);
  165.                                 }
  166.                                 else
  167.                                 {
  168.                                     bw.write(currentLine);
  169.                                     System.out.println("Lassen: "+currentLine);
  170.                                 }
  171.                                 bw.newLine();
  172.                            }
  173.                        }
  174.                    }    
  175.                    
  176.                    
  177.                    
  178.                    break;
  179.                }
  180.             }
  181.         }
  182.        
  183.         if(foundkeybool == true)
  184.         {
  185.            tmp.setWritable(true);
  186.            pfile.setWritable(true);
  187.            boolean removed = dini_Remove(filepath);
  188.            boolean renamed = tmp.renameTo(pfile);
  189.         }
  190.         else //(foundkeybool == false) if key does not exists we can create a new one
  191.         {
  192.             try(BufferedWriter bw = new BufferedWriter(new FileWriter(pfilepath,true)))
  193.             {
  194.                 bw.write(key.concat("=").concat(value));
  195.                 bw.newLine();
  196.                 bw.close();
  197.             }    
  198.         }  
  199.         return true;
  200.     }
  201.    
  202.     /**
  203.      * Diese Methode liest aus einer Datei den String Wert hinter einem Key aus
  204.      *
  205.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  206.      * @param Der auszulesende Key
  207.      * @return Der String welcher unter dem Key gespeichert wurde bzw. null wenn der Key nicht existiert.
  208.      */
  209.     public static String dini_String(String filepath, String key) throws IOException
  210.     {
  211.         if(key.length() <= 0) return null;
  212.  
  213.         String pfilepath = rootdirectory.concat(filepath);
  214.         File pfile = new File(pfilepath);
  215.         //dini_Remove(filepath.concat(".part"));
  216.        
  217.         if(dini_Exists(filepath) == false) return null;
  218.        
  219.         boolean foundkeybool = false;
  220.         String returnstring = null;
  221.        
  222.         try(BufferedReader br = new BufferedReader(new FileReader(pfilepath)))
  223.         {          
  224.             String line = null;
  225.             while((line = br.readLine()) != null)
  226.             {
  227.                String foundkey = line.substring(0,line.indexOf("="));
  228.                if(foundkey.equals(key))
  229.                {
  230.                    foundkeybool = true;
  231.                    returnstring = line.substring(line.indexOf("=")+1,line.length());
  232.                }
  233.             }
  234.         }    
  235.         if(foundkeybool == true)
  236.         {
  237.             return returnstring;
  238.         }
  239.         else
  240.         {
  241.             return null;
  242.         }
  243.     }
  244.    
  245.     /**
  246.      * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Integer
  247.      *
  248.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  249.      * @param Key Die Key Flag für den Value
  250.      * @param Value Zu speichernder Integer
  251.      * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
  252.      */
  253.     public static boolean dini_IntSet(String filepath, String key,int value) throws IOException
  254.     {
  255.         Integer valueint = new Integer(value);
  256.         return dini_StringSet(filepath,key,valueint.toString());
  257.     }
  258.    
  259.     /**
  260.      * Diese Methode liefert einen Integer Wert, welcher unter einem Key gespeichert ist
  261.      *
  262.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  263.      * @param Key Die Key Flag für den Integer
  264.      * @return Den Integer Wert bzw. Integer.MIN_VALUE bei einem Fehler
  265.      */
  266.     public static int dini_Int(String filepath, String key) throws IOException
  267.     {
  268.         String intString = dini_String(filepath,key);
  269.         int val;
  270.         try
  271.         {
  272.             val = Integer.parseInt(intString);
  273.             return val;
  274.         }
  275.         catch (NumberFormatException nfe)
  276.         {
  277.             return Integer.MIN_VALUE;
  278.         }
  279.     }
  280.    
  281.     /**
  282.      * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Boolean
  283.      *
  284.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  285.      * @param Key Die Key Flag für den Value
  286.      * @param Value Zu speichernder Boolean
  287.      * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
  288.      */
  289.     public static boolean dini_BoolSet(String filepath, String key,boolean value) throws IOException
  290.     {
  291.         Boolean valuebool = new Boolean(value);
  292.         return dini_StringSet(filepath,key,valuebool.toString());
  293.     }
  294.    
  295.     /**
  296.      * Diese Methode liefert einen Boolean Wert, welcher unter einem Key gespeichert ist
  297.      *
  298.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  299.      * @param Key Die Key Flag für den Boolean
  300.      * @return Den Boolean Wert
  301.      */
  302.     public static boolean dini_Bool(String filepath, String key) throws IOException
  303.     {
  304.         String boolString = dini_String(filepath,key);
  305.         boolean val;
  306.        
  307.         if("true".equals(boolString) || "false".equals(boolString))
  308.         {
  309.             val = Boolean.parseBoolean(boolString);
  310.         }
  311.         else
  312.         {
  313.             val = false;
  314.         }
  315.         return val;
  316.     }
  317.    
  318.     /**
  319.      * Diese Methode erstellt in der angegebenen Datei einen Key (Flag) mit dem angegebenen Float
  320.      *
  321.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  322.      * @param Key Die Key Flag für den Value
  323.      * @param Value Zu speichernder Float
  324.      * @return boolean true - Wenn erfolgreich unter dem Key gespeichert, false - Wenn nicht erfolgreich gespeichert
  325.      */
  326.     public static boolean dini_FloatSet(String filepath, String key,float value) throws IOException
  327.     {
  328.         Float valuefloat = new Float(value);
  329.         return dini_StringSet(filepath,key,valuefloat.toString());
  330.     }
  331.    
  332.     /**
  333.      * Diese Methode liefert einen Float Wert, welcher unter einem Key gespeichert ist
  334.      *
  335.      * @param filepath Der Dateipath, ausgehend vom Programm Root Path
  336.      * @param Key Die Key Flag für den Float
  337.      * @return Den Float Wert oder den MIN_VALUE bei einem Fehler
  338.      */
  339.     public static float dini_Float(String filepath, String key) throws IOException
  340.     {
  341.         String floatString = dini_String(filepath,key);
  342.         float val;
  343.         try
  344.         {
  345.             val = Float.parseFloat(floatString);
  346.             return val;
  347.         }
  348.         catch (NumberFormatException nfe)
  349.         {
  350.             return Float.MIN_VALUE;
  351.         }
  352.     }
  353. }
Add Comment
Please, Sign In to add comment