Java Operations Properties FileIn Java.The properties file is a configuration file that is used primarily to express configuration information with a file type of *.Properties, formatted as text files, the contents of the file are formatted as "key = value" in the format,Properties
File, you can use "#" to make comments,Properties file onJava programming in a lot of places, easy to operate.
OneProperties file
Test.Properties
------------------------------------------------------
#################################
# Industry and Commerce Report Application icisreport config file #
# Date: November 21, 2006 #
#################################
#
# Description: Business system TOPICIS and Reporting system icisreport are separate
# can be deployed separately to different servers, or can be deployed to the same service
Icisreprot as a standalone web application can use any
# The servlet container or the Java EE Server is deployed and run separately, or you can
# is applied as a library of business systems through interface calls to business systems.
#
# Icisreport's IP
icisreport.server.ip=192.168.3.143
# Icisreport Port
icisreport.server.port=8080
# Context path for Icisreport
Icisreport.contextpath=/icisreport
------------------------------------------------------
Important methods of the properties class
The Properties class exists in the cellJava.util, this class inherits from the Hashtable
1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. Load (InputStream instream) to read the list of attributes (key and element pairs) from the input stream. Through the specified file (for example, the above test.Properties file) to be loaded for this article
All key-value pairs in a piece. To search for GetProperty (String key).
3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.
4. Store (OutputStream out, String comments) to fit with the Load method loaded into theFormat in the Properties table, set thisList of attributes in the properties table (keys and elements
Write to the output stream. In contrast to the load method, the method writes a key-value pair to the specified file.
5. Clear () Clears all loaded key-value pairs. This method is provided in the base class.
-------------------------------
Second, the operationof the properties fileJava methods
Read Properties File
Properties prop = newProperties ();
InputStream in = GetClass (). getResourceAsStream ("/icisreport.Properties ");
Prop.load (in);
Set KeyValue = Prop.keyset ();
for (Iterator it = Keyvalue.iterator (); It.hasnext ();)
{
String key = (string) it.next ();
}
------------------------
OutputFile = new FileOutputStream (fileName);
Propertie.store (outputFile, description);
Outputfile.close ();
-----------------------------------------------------------------------------------------
Class.getresourceasstream ("/some/pkg/resource.Properties ");
Classloader.getresourceasstream ("Some/pkg/resource.properties ");
java.util.resourcebundle rs = Java.util.ResourceBundle.getBundle ("Some.pkg.resource");
Rs.getstring ("Xiaofei");
-----------------------------------------------------------------------------------------
Write properties file
Configuration SAVECF = new configuration ();
Savecf.setvalue ("Min", "10");
Savecf.setvalue ("Max", "1000");
Savecf.savefile (". \config\save.perperties", "Test");
Summary: java properties files need to be placed under classpath so that the program can read, About Classpath is actually java class or library of the storage path, in java project, The properties are placed in a block of the
class file. In the Web application, the simplest way is to put it in the web-inf\classes directory of the Web application, or under Other folders, when you need to set the CLASSPATH environment variable
, add this folder path to In the CLASSPATH variable, this can also be read. Here, you need to have a deep understanding of classpath, classpath is not deliberately set in the system environment of the system changed
Volume, web-inf\classes In fact, theJava Project class file directory is also.
Send an example of everyone to see ha.
Package control;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import java.util.Properties;
public class Testmain {
Read value by key
public static string ReadValue (String filepath,string key) {
Properties Props = new properties ();
try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
String value = Props.getproperty (key);
System.out.println (Key+value);
return value;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}
Read all information about the properties
public static void ReadProperties (String filePath) {
Properties Props = new properties ();
try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
Enumeration en = Props.propertynames ();
while (En.hasmoreelements ()) {
String key = (string) en.nextelement ();
String property = Props.getproperty (key);
System.out.println (Key+property);
}
} catch (Exception e) {
E.printstacktrace ();
}
}
Write Properties Information
public static void WriteProperties (String filepath,string parametername,string parametervalue) {
Properties prop = new properties ();
try {
InputStream fis = new FileInputStream (FilePath);
Read the list of attributes from the input stream (key and element pairs)
Prop.load (FIS);
Call the Hashtable method put. Use the GetProperty method to provide parallelism.
Enforces the use of strings for the keys and values of the properties. The return value is the result of the Hashtable call put.
OutputStream fos = new FileOutputStream (FilePath);
Prop.setproperty (ParameterName, ParameterValue);
In a format that is appropriate for loading into the Properties table using the Load method,
Writes the list of attributes (key and element pairs) in this properties table to the output stream
Prop.store (FOS, "Update" + parametername + "' value");
} catch (IOException e) {
System.err.println ("Visit" +filepath+ "for updating" +parametername+ "value error");
}
}
public static void Main (string[] args) {
ReadValue ("info.properties", "url");
WriteProperties ("Info.properties", "Age", "21");
ReadProperties ("Info.properties");
System.out.println ("OK");
}
Send an example of everyone to see ha.
Package control;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import java.util.Properties;
public class Testmain {
Read value by key
public static string ReadValue (String filepath,string key) {
Properties Props = new properties ();
try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
String value = Props.getproperty (key);
System.out.println (Key+value);
return value;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}
Read all information about the properties
public static void ReadProperties (String filePath) {
Properties Props = new properties ();
try {
InputStream in = new Bufferedinputstream (new FileInputStream (FilePath));
Props.load (in);
Enumeration en = Props.propertynames ();
while (En.hasmoreelements ()) {
String key = (string) en.nextelement ();
String property = Props.getproperty (key);
System.out.println (Key+property);
}
} catch (Exception e) {
E.printstacktrace ();
}
}
Write Properties Information
public static void WriteProperties (String filepath,string parametername,string parametervalue) {
Properties prop = new properties ();
try {
InputStream fis = new FileInputStream (FilePath);
Read the list of attributes from the input stream (key and element pairs)
Prop.load (FIS);
Call the Hashtable method put. Use the GetProperty method to provide parallelism.
Enforces the use of strings for the keys and values of the properties. The return value is the result of the Hashtable call put.
OutputStream fos = new FileOutputStream (FilePath);
Prop.setproperty (ParameterName, ParameterValue);
In a format that is appropriate for loading into the Properties table using the Load method,
Writes the list of attributes (key and element pairs) in this properties table to the output stream
Prop.store (FOS, "Update" + parametername + "' value");
} catch (IOException e) {
System.err.println ("Visit" +filepath+ "for updating" +parametername+ "value error");
}
}
public static void Main (string[] args) {
ReadValue ("info.properties", "url");
WriteProperties ("Info.properties", "Age", "21");
ReadProperties ("Info.properties");
System.out.println ("OK");
}
}
Java Operations Properties File