[[oktatas:programozás:java|< Java]] ====== Java config fájl ====== * **Szerző:** Sallai András * Copyright (c) 2014, Sallai András * Szerkesztve: 2014, 2023 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== A beállításokról ===== A programok számára tetszőleges névvel és kiterjesztéssel létrehozhatunk beállításokat tartalmazó fájlokat. Ezek lehetnek például .config vagy .properties kiterjesztésű állományok. ===== Egyszerű példa ===== Program.Nev = Teszt Program Program.Verzio = 1.0 A beállításokat beolvashatjuk egy Properties objektumba, illetve ki is írhatjuk azokat. import java.util.*; import java.io.*; class Program { public static void main(String[] argv) throws Exception { Properties beallitasok = new Properties(); String fajlNev = "Program.config"; InputStream befolyam = new FileInputStream(fajlNev); beallitasok.load(befolyam); System.out.println(beallitasok.getProperty("Program.Nev")); System.out.println(beallitasok.getProperty("Program.Verzio")); } } ===== Beállítások fájlba írása/olvasása ===== ==== Kiírás ==== import java.util.Properties; import java.io.FileOutputStream; import java.io.IOException; class Program01 { public static void main(String args[]) { Properties pro = new Properties(); pro.setProperty("Első", "Nagy Lajos"); pro.setProperty("Második", "Kerek Béla"); try { pro.store(new FileOutputStream("config.properties"), null); }catch(IOException e) { System.err.println("Hiba a kiírás során!"); } } } A null helyére String típusú megjegyzést írhatok. ==== Beolvasás ==== import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; class Program01 { public static void main(String args[]) { Properties pro = new Properties(); try { pro.load(new FileInputStream("config.properties")); System.out.println(pro.getProperty("Első")); System.out.println(pro.getProperty("Második")); }catch(IOException e) { System.err.println("Hiba a betöltés során!"); } } } ===== Osztállyal azonos útvonalról ===== ==== Olvasás ==== import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; class Program01 { public static void main(String args[]) { Properties pro = new Properties(); try { pro.load(Program02.class.getClassLoader().getResourceAsStream("config.properties")); System.out.println(pro.getProperty("Első")); System.out.println(pro.getProperty("Második")); }catch(IOException e) { System.err.println("Hiba a betöltés során!"); } } } ==== Olvasás példányosítás esetén ==== import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; class Program01 { public static void main(String args[]) { Options op = new Options(); op.oLoad(); } } class Options { public void oLoad() { Properties pro = new Properties(); try { pro.load(getClass().getClassLoader().getResourceAsStream("config.properties")); System.out.println(pro.getProperty("Első")); System.out.println(pro.getProperty("Második")); }catch(IOException e) { System.err.println("Hiba a betöltés során!"); } } } ===== UTF-8 használata ===== ==== UTF-8 kiírása ==== import java.util.Properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; class Program05 { public static void main(String args[]) { Properties pro = new Properties(); pro.setProperty("Első", "Nagy Lajos"); pro.setProperty("Második", "Kerek Béla"); try { FileOutputStream f = new FileOutputStream("config.properties"); OutputStreamWriter o = new OutputStreamWriter(f, "UTF-8"); Writer out = new BufferedWriter(o); pro.store(out, null); }catch(IOException e) { System.err.println("Hiba a kiírás során!"); } } } ==== UTF-8 olvasása ==== import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; import java.io.Reader; import java.io.BufferedReader; import java.io.InputStreamReader; class Program06 { public static void main(String args[]) { Properties pro = new Properties(); try { FileInputStream f = new FileInputStream("config.properties"); InputStreamReader i = new InputStreamReader(f, "UTF-8"); Reader in = new BufferedReader(i); pro.load(in); System.out.println(pro.getProperty("Első")); System.out.println(pro.getProperty("Második")); }catch(IOException e) { System.err.println("Hiba a betöltés során!"); } } } ==== Kódolás ellenőrzése ==== import java.util.Properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; class Program05 { public static void main(String args[]) { Properties pro = new Properties(); pro.setProperty("Első", "Nagy Lajos"); pro.setProperty("Második", "Kerek Béla"); try { FileOutputStream f = new FileOutputStream("config.properties"); OutputStreamWriter o = new OutputStreamWriter(f, "UTF-8"); Writer out = new BufferedWriter(o); pro.store(out, null); }catch(UnsupportedEncodingException e) { System.err.println("Hibás kódolás!"); }catch(IOException e) { System.err.println("Hiba a kiírás során!"); } } } ===== Properties és ResourceBundle ===== A properties és a ResourceBundle használható együtt. A ResourceBunle viszont arra való, hogy nyelvfüggő beállításokat tároljunk. import java.util.ResourceBundle; import java.util.Properties; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; class Program01 { public static void main(String[] args) throws FileNotFoundException, IOException { ResourceBundle rb = ResourceBundle.getBundle("Program01"); System.out.println(rb.getString("Program.Verzio")); Properties p = new Properties(); InputStream fin = new FileInputStream("Program01.config"); p.load(fin); System.out.println(p.getProperty("Program.Verzio")); } } ===== Linkek ===== * http://docs.oracle.com/javase/6/docs/api/index.html?java/util/Properties.html * http://docs.oracle.com/javase/7/docs/api/index.html?java/util/Properties.html * http://docs.oracle.com/javase/8/docs/api/index.html?java/util/Properties.html * https://docs.oracle.com/en/java/javase/17/docs/api/index.html?java/util/Properties.html