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); }
}