Java Properties File Örneği – Kullanımı ( Java Properties File Examples )
Properties File özel bir dosya türüdür. Kaydedilme türü config.properties ‘dir. Genellikle ‘özellik bilgileri’ depolamak amacı ile kullanılır. Örneğin ben veri tabanı bilgilerimin özelliklerimi tutmak istediğimi varsayıyorum..
1.Aşama Properties File ‘ e yazma işlemi
-
public class OrnekPropertiesKullanimi
-
{
-
public static void main( String[] args )
-
{
-
Properties prop = new Properties();
-
-
prop.setProperty("database", "localhost");
-
prop.setProperty("dbuser", "kullaniciAdi");
-
prop.setProperty("dbpassword", "sifre");
-
-
prop.store(new FileOutputStream("config.properties"), null);
-
-
-
}
-
}
Bu uygulama sonrası config.properties içerisine eklenen bilgiler aşağıdaki gibidir.
-
database=localhost
-
dbuser=kullaniciAdi
-
dbpassword=sifre
Burada config.properties ‘e bilgileri ekledik. Peki bu config.properties dosyasını kullanmak istedğimde yapmam gereken nedir?
-
public class OrnekPropertiesKullanimi2
-
{
-
public static void main( String[] args )
-
{
-
Properties prop = new Properties();
-
-
prop.load(new FileInputStream("config.properties"));
-
-
System.out.println(prop.getProperty("database"));
-
System.out.println(prop.getProperty("dbuser"));
-
System.out.println(prop.getProperty("dbpassword"));
-
-
}
-
}
Yukarıdaki uygulamamın çıktısı ;
-
localhost
-
kullaniciAdi
-
sifre
şeklinde olacaktır..
Bana ulaşın