Java Operations Properties File

Source: Internet
Author: User
Tags list of attributes

Java Operations Properties FileJavaIn thePropertiesA file is a configuration file that is used primarily to express configuration information with a file type of *.Properties, the format is a text file, the content of the file is formatted as a "key = value" format,Properties

File, you can use "#" to make comments,PropertiesFile inJavaThere are many places in programming, and it is very convenient to operate.
OnePropertiesFile

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

------------------------------------------------------
PropertiesImportant methods of the class
Propertiesclass exists in the cellJavaIn. Util, this class inherits from 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.Propertiesfile) is loaded to get the 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 thePropertiesFormat in the table, set thisPropertiesList of attributes in a 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 operationPropertiesof the fileJavaMethod

Read Properties File
PropertiesProp = 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");

Summarize:JavaOfPropertiesThe file needs to be placed under classpath so that the program can read, and the classpath is actuallyJavaThe storage path for a class or library,JavaIn the project,PropertiesPut in

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

, this folder path is added to the CLASSPATH variable, which can also be read. Here, you need to have a deep understanding of classpath, classpath is not deliberately set in the system of the system environment changes

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.