about how to read and modify values in the. properties file in Java

Source: Internet
Author: User

The first way:

With my own project as a prototype here's a simple way to get the case

Define a Fetch class

Package com.boli.tianchuang.sys.util;
Import java.io.*;

Import java.util.Properties;
    /** * Desc:properties File Acquisition Tool class * Created by/public class Propertyutil {private static Properties props;
    static{Loadprops ();
        } synchronized static private void Loadprops () {props = new Properties ();
        InputStream in = null; try {//First, get the properties file stream through the ClassLoader//in = PropertyUtil.class.getClassLoader (). getResourceAsStream ("Sysc
            Onfig.properties ");
            The second is to get the properties file stream in = PropertyUtil.class.getResourceAsStream ("/properties/sysconfig.properties") through the class;
        Props.load (in);
        catch (FileNotFoundException e) {//logger.error ("Sysconfig.properties File not Found");
        catch (IOException e) {//logger.error ("appear ioexception");
                Finally {try {if (null!= in) {in.close (); catch (IOExceptione) {//logger.error ("Sysconfig.properties file stream shutdown occurrence exception");
        }//logger.info ("Load the contents of the properties file ...");
    Logger.info ("Properties File content:" + props);
        public static string GetProperty (string key) {if (null = props) {loadprops ();
    return Props.getproperty (key); public static string GetProperty (string key, String defaultvalue) {if (null = = props) {Loadpro
        PS ();
    Return Props.getproperty (key, DefaultValue); }
}


Here is the file directory for my project

The above class can get value directly from the key.

Special attention here is the file path of your project, here must be written right.


The second way:

The file directory is the same as the above, but I have added and modified features here, and here are the codes and explanations

Package com.boli.tianchuang.helper;  
Import Java.io.FileInputStream;  
Import java.io.FileNotFoundException;  
Import Java.io.FileOutputStream;  
Import java.io.IOException;

Import java.util.Properties;
	
	public class Ceshi {private static Properties prop; public static void Load (String path) {//The path here is the absolute path of the project file//Get the Project absolute path First: Thread.CurrentThread (). Getcontextclassloader ()
		. GetResource (""). GetPath ();
		Then the "Properties/sysconfig.properties" is spliced behind the project path;  
        Prop= new properties ()//Property collection Object FileInputStream fis;
        	try {System.out.println (path);
            FIS = new FileInputStream (path);
            Prop.load (FIS);  
        Fis.close ();//Close Stream} catch (FileNotFoundException e) {e.printstacktrace ();  
        catch (IOException e) {e.printstacktrace ();
    	}//parameter is the file path to be modified and the property name and property value to be modified public static Boolean Updatepro (String path,string key,string value) {
   if (prop==null) {load (path); 		System.out.println ("Reload again before modification");
        System.out.println ("Gets the property value before adding or modifying:" +key+ "=" + Prop.getproperty (key));   
        Prop.setproperty (key, value);   
            File output stream try {fileoutputstream fos = new FileOutputStream (path);   
            Save the Properties collection to the stream Prop.store (Fos, "Copyright (c) boxcode Studio");  
            Fos.close ()//Close stream} catch (FileNotFoundException e) {//TODO auto-generated catch block  
            E.printstacktrace ();  
        return false;  
            catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();  
        return false;
        System.out.println ("Get the added or modified property value:" +key+ "=" + Prop.getproperty (key));  
    return true;
    		}//parameter is the file path to be modified and the property name and property value to be modified public static string Getpro (String path,string key) {if (prop==null) {
    		Load (path);
    	System.out.println ("Reload Again");
}        FileInputStream fis;  
            try {fis = new FileInputStream (path);  
            Prop.load (FIS)//Load the property file stream into the Properties object Fis.close ()//Close stream} catch (FileNotFoundException e) {  
        E.printstacktrace ();  
        catch (IOException e) {e.printstacktrace ();
        } System.out.println ("+key+" value of the query: +prop.getproperty (key));  
    return Prop.getproperty (key); }  
}


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.