"Dina iki kita njelajah topik anyar lan menarik liyane: properti. "
![](https://cdn.codegym.cc/images/article/eedcbd3f-3063-42a1-aa33-72e687ba42a1/800.jpeg)
"Ing Jawa, biasane nggawe program sing fleksibel lan gampang disesuaikan, yaiku gampang dikonfigurasi."
"Contone, sapisan saben jam program sampeyan nyalin file saka direktori tartamtu, zip, lan dikirim menyang sampeyan ing email. Kanggo nindakake iki, program kudu ngerti direktori ngendi file bakal dijupuk, lan alamat email. data kasebut biasane disimpen ora ing kode aplikasi, nanging ing file properti sing kapisah ."
Data ing file iki disimpen minangka pasangan kunci-nilai, dipisahake kanthi tandha sing padha.
data.properties file
directory = c:/text/downloads
email = zapp@codegym.cc
"Ing sisih kiwa tandha ana jeneng (kunci), ing sisih tengen ana regane."
"Dadi iki minangka perwakilan tekstual saka HashMap?"
"Secara umum, ya."
"Kanggo gampang nggarap file kasebut, Jawa duwe kelas Properties khusus. Kelas Properti duwe warisan Hashtable<Object,Object>. Malah bisa dianggep minangka Hashtable sing bisa mbukak dhewe saka file."
"Iki cara-carane:"
Metode | Katrangan |
---|---|
void load(Reader reader) |
Muat properti saka file sing diwakili dening obyek Reader |
void load(InputStream inStream) |
Muat properti saka file sing diwakili dening obyek InputStream |
void loadFromXML(InputStream in) |
Muat properti saka file XML |
Object get(Object key) |
Ngasilake nilai kanggo kunci sing ditemtokake. Cara iki diwarisake saka Hashtable |
String getProperty(String key) |
Ngasilake nilai properti (string) kanthi kunci. |
String getProperty(String key, String defaultValue) |
Ngasilake nilai properti kanthi kunci utawa defaultValue yen ora ana kunci kasebut. |
Set<String> stringPropertyNames() |
Ngasilake dhaptar kabeh tombol |
"Ing tembung liyane, sampeyan kudu nindakake mung rong operasi - mbukak data saka sawetara file menyang obyek Properties banjur entuk properti kasebut nggunakake metode getProperty (). Inggih, lan aja lali yen sampeyan bisa nggunakake obyek Properties kaya. HashMap . "
"Iki conto liyane:"
// The file that stores our project's properties
File file = new File("c:/data.properties");
// Create a Properties object and load data from the file into it.
Properties properties = new Properties();
properties.load(new FileReader(file));
// Get property values from the Properties object
String email = properties.getProperty("email");
String directory = properties.getProperty("directory");
// Get a numeric value from the Properties object
int maxFileSize = Integer.parseInt(properties.getProperty("max.size", "10000"));
"Ah. Dadi kita nggawe obyek Properties, lan banjur pass file menyang. Kanggo cara mbukak. Banjur kita mung nelpon getProperty. tengen?"
"Ya wis."
"Lan sampeyan uga ujar manawa bisa digunakake minangka HashMap? Apa tegese?"
"Kelas Properties marisi Hashtable, sing padha karo HashMap, nanging kabeh cara disinkronake. Sampeyan mung bisa nampilake kabeh nilai saka file properti ing layar kaya iki:"
// Get the file with the properties
File file = new File("c:/data.properties");
// Create a Properties object and load data from the file into it.
Properties properties = new Properties();
properties.load(new FileReader(file));
// Run through all the keys and display their values on the console
for (String key : properties.stringPropertyNames())
{
System.out.println(properties.get(key));
}
"Ah. Kabeh kayane wis tiba. Matur nuwun, Rishi. Aku bakal nggunakake barang sing keren iki."
GO TO FULL VERSION