İçindekilerGirişİndex
YukarıİlkÖncekiSonraki YokSon
Geriİleri
Yazdır

Properties Class'ı

Properties Class'ı ve Kullanıldığı Yerler

Özetle anlatmak gerekirse, Properties class'ı bir takım anahtarlara karşılık gelen değerlerin diskte saklanması ve okunmasına yarar. Bu class Hashtable'ı extend etmiş, sadece anahtarların ve değerlerin tipini String'le sınırlamış ve diske okuma yazma işlevi eklemiştir.

Properties class'ı en çok konfigurasyon, ayarlar ve seçenekler için kullanılır. Bir programın ayarları bir xxx.properties dosyasında durur. Dosyanın içinde :

font=Arial
background=red
logo=/img/mylogo.gif

gibi satırlar bulunur. Sol taraftaki değerler key (anahtar), sağ taraftakiler value (değerdir). Bu ikiliye özellik (property) denir:

Properties Kullanım Örnekleri

Properties class'yla yaratma, yükleme ve değiştirme işlemler yapılabilir.

Properties Yaratma

Properties dosyası yaratmak için şu şekilde bir kod kullanılır.

    Properties properties=new Properties();
    properties.setProperty("font","Arial");
    properties.setProperty("background","red");
    properties.setProperty("logo","/img/mylogo.gif");    
    FileOutputStream fos=new FileOutputStream("c:\\myfolder\\myfile.properties");
    properties.store(fos,"");            
    fos.close(); 

Burada Properties nesnesi yaratılmakta, belli anahtarlara karşılık belli değerler vererk property'ler atanmakta ve bir dosyaya saklanmaktadır.

Properties dosyayı yaratmak için ille deprogramlama yapmak gerkemez. Bir metin dosyası açıp her satıra bir property'yi key=value şeklinde koyarsanız zaten yaratılmış olur. Bu dosyalar elle hazırlanmak ve değiştirilmek üzere düşünülmüştür.

Properties Yükleme

Proeprties class'ının içine veri yüklemek için şöyle bir kod parçası yazılabilir :

    Properties properties=new Properties();    
    FileInputStream fis=new FileInputStream("c:\\myfolder\\myfile.properties");
    properties.load(fis);   
    fis.close();
    String font=properties.getProperty("font");
    System.out.println("font : "+font);

Bu kod, bir Properties nesnesi yaratmakta onu bir dosyadan doldurmakta ve içerisinde bir property'nin değerini almaktadır.

Properties Değiştirme

Bir properties dosyasını yüklemek içerisindeki property'lerin değerini değiştirmek için aşağıdakine benzer bir kod yazılır :

    Properties properties=new Properties();    
    FileInputStream fis=new FileInputStream("c:\\myfolder\\myfile.properties");
    properties.load(fis);   
    fis.close();
    properties.setProperty("font","Times New Roman");
    properties.setProperty("width","400");
    properties.setProperty("height","600");
    FileOutputStream fos=new FileOutputStream("c:\\myfolder\\myfile.properties");
    properties.store(fos,"");            
    fos.close();      

Bu kod, önce properties değerlerini yüklüyor, sonra değiştiriyor, en sonunda da dosyaya kaydediyor.

Bütün Properties kuulanımını toplu halde gösteren bir örnek :

PropertiesTest.javaİndir Göster Gizle Kopar Satır Gizle Satır Göster
  1 package com.godoro.samples.util;
  2 import java.util.*;
  3 import java.io.*;
  4 public class PropertiesTest {
  5   public static void createTest(String path)
  6     throws IOException
  7   { 
  8     Properties properties=new Properties();
  9     properties.setProperty("font","Arial");
 10     properties.setProperty("background","red");
 11     properties.setProperty("logo","/img/mylogo.gif");    
 12     FileOutputStream fos=new FileOutputStream(path);
 13     properties.store(fos,"");            
 14     fos.close();    
 15   }
 16   public static void loadTest(String path)
 17     throws IOException
 18   { 
 19     Properties properties=new Properties();    
 20     FileInputStream fis=new FileInputStream(path);
 21     properties.load(fis);   
 22     fis.close();
 23     String font=properties.getProperty("font");
 24     System.out.println("font : "+font);
 25   }
 26   public static void changeTest(String path)
 27     throws IOException
 28   { 
 29     Properties properties=new Properties();    
 30     FileInputStream fis=new FileInputStream(path);
 31     properties.load(fis);   
 32     fis.close();
 33     properties.setProperty("font","Times New Roman");
 34     properties.setProperty("width","400");
 35     properties.setProperty("height","600");
 36     FileOutputStream fos=new FileOutputStream(path);
 37     properties.store(fos,"");            
 38     fos.close();        
 39   }
 40   
 41   public static void main(String[] args)
 42     throws IOException
 43   { 
 44     String path="C:\\Godoro\\Projects\\Java\\Education\\JavaBook\\test\\test.properties";    
 45     //createTest(path);
 46     loadTest(path);
 47     //changeTest(path);
 48     
 49   }  
 50   
 51 }

Dosya Listesi

İçindekilerGirişİndex
YukarıİlkÖncekiSonraki YokSon
Geriİleri
Yazdır