java properties 操作-

 Java中提供了一个java.util.Properties工具类,使用Properties类您可以方便的从一个.properties属性文件中读取设置参数,示例代码如下:
Properties props = new Properties();
props.load(new FileInputStream("filename.properties"));
String value = props.getProperty("propertyname");
如果您的.properties文件打包入一个Jar或War文件,您可以使用ClassLoader的getResourceAsStream()方法得到一个InputStream对象,示例代码如下:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("com/company/application/application.properties"));
String value = props.getProperty("propertyname"); 

 

 

 

java写properties文件的方法
    用Java写properties文件时,如果直接用setProperties和store方法往FileOutputStream写,写出来的东西面目全非了。没有正确的格式。因此我使用了这个方法来写,一行一行的读。一行一行地写。   1/**//**
  2   * write property.
  3   * @param title parameter defined in properties file
  4   * @param key parameter defined title value
  5   * @return String return value
  6   */
  7  String writeProp(String filePath, String fileName, String title, String key,
  8                   Logger logger) {
  9
 10    String strResult = "";
 11    String pathAddFile = ""; //write file with path and name
 12    String tempFile = "";
 13    String strTemp = ""; //use for identify if the modify is success
 14    //filePath is null the file in the default path ,else file in the filePath+/+fileName
 15    if (filePath.equals("")) {
 16      pathAddFile = fileName;
 17      tempFile = "temp.properties";
 18    }
 19    else {
 20      pathAddFile = filePath + systemSeparator + fileName;
 21      tempFile = filePath + systemSeparator + "temp.properties";
 22    }
 23    //properties file
 24    File aFile = new File(pathAddFile);
 25    //temp file
 26    File tFile = new File(tempFile);
 27    if (!aFile.exists()) {

 31      strResult = "error";
 32      return strResult;
 33    }
 34    //set property to properties
 35    try {
 36      FileReader fr = new FileReader(pathAddFile);
 37
 38      BufferedReader br = new BufferedReader(fr);
 39      try {
 40        FileWriter fw = new FileWriter(tempFile);
 41        PrintWriter out = new PrintWriter(fw);
 42
 43        String strLine = br.readLine().trim();
 44        while (strLine != null) {
 45          //identify if strLine have title,have change key
 46          if (strLine.startsWith(title)) {
 47            strLine = title + "=" + key;
 48            strTemp = "1";
 49          }
 50          out.write(strLine);
 51          out.println();
 52          out.flush();
 53          //read next line
 54          strLine = br.readLine();
 55        }
 56        fw.close();
 57        out.close();
 58        //close BufferedReader object
 59        br.close();
 60         //close file
 61        fr.close();
 62        //delete properties file
 63        if (aFile.exists()) {
 64          if (!aFile.delete()) {
 68            return "error";
 69          }
 70        }
 71        //rename temp file to properties file
 72        if (!tFile.exists()) {
 76          return "error";
 77        }
 78        tFile.renameTo(aFile);
 79        if (!strTemp.equals("1")) {
 80          //there is no title prop exit so modify failed
 85          strResult = "error";
 86
 87        }
 88        return strResult;
 89      }
 90      catch (IOException ex2) {
 91        ex2.printStackTrace();
 92        strResult = "error";
 93        logger.fatal(
 94            "CmnEToyotaExtractProp ----- writeProp ----- failed !");
 95        return strResult;
 96
 97      }
 98    }
 99    catch (FileNotFoundException ex1) {
100      ex1.printStackTrace();

103      strResult = "error";
104      return strResult;
105    }
106
107  }

 

 

 

 

 

 

 

java中Properties类的使用
<script language="javascript" src="/ad/js/edu_left_300-300.js" type="text/javascript"></script>
package com.adrop.util;



import java.io.*;

import java.util.Properties;

import javax.servlet.http.*;

import javax.servlet.*;

import javax.servlet.jsp.*;



public class PropertiesUtil {

private String fileName;

private Properties p;

private FileInputStream in;

private FileOutputStream out;

/**

* 根据传进的文件名载入文件

* @param fileName String

*/

public PropertiesUtil(String fileName) {

this.fileName=fileName;

File file = new File(fileName);

try {

in = new FileInputStream(file);

p = new Properties();

//载入文件

p.load(in);

in.close();

}

catch (FileNotFoundException e) {

System.err.println("配置文件config.properties找不到!!");

e.printStackTrace();

}

catch (Exception e) {

System.err.println("读取配置文件config.properties错误!!");

e.printStackTrace();

}

}



/**

* 配置文件一律为config.propertities,并且统一放在web应用的根目录下。

* @return String

*/

public static String getConfigFile(HttpServlet hs) {



return getConfigFile(hs,"config.properties");

}

/**

* 在servlet中使用,直接用this作为参数,HttpServlet类型

* 根据配置文件名从当前web应用的根目录下找出配置文件

* @param hs HttpServlet

* @param configFileName String配置文件名字

* @return String

*/

public static String getConfigFile(HttpServlet hs, String configFileName) {

String configFile = "";

ServletContext sc = hs.getServletContext();

configFile = sc.getRealPath("/" + configFileName);

if (configFile == null || configFile.equals("")) {

configFile = "/" + configFileName;

}

return configFile;

}

/**

* jsp中用pageContext作参数

* @param hs PageContext

* @param configFileName String 配置文件名字

* @return String

*/

public static String getConfigFile(PageContext hs, String configFileName) {

String configFile = "";

ServletContext sc = hs.getServletContext();

configFile = sc.getRealPath("/" + configFileName);

if (configFile == null || configFile.equals("")) {

configFile = "/" + configFileName;

}

return configFile;

}



/**

* 列出所有的配置文件内容

*/

public void list() {

p.list(System.out);

}



/**

* 指定配置项名称,返回配置值

* @param itemName String

* @return String

*/

public String getValue(String itemName){

return p.getProperty(itemName);

}



/**

* 指定配置项名称和默认值,返回配置值

* @param itemName String

* @param defaultValue String

* @return String

*/

public String getValue(String itemName,

String defaultValue){

return p.getProperty(itemName,defaultValue);

}



/**

* 设置配置项名称及其值

* @param itemName String

* @param value String

*/

public void setValue(String itemName,String value){

p.setProperty(itemName,value);

return;

}



/**

* 保存配置文件,指定文件名和抬头描述

* @param fileName String

* @param description String

* @throws Exception

*/

public void saveFile(String fileName,String description)throws Exception{

try {

File f=new File(fileName);

out

= new FileOutputStream(f);

p.store(out, description);//保存文件

out.close();

}

catch (IOException ex) {

throw new Exception

("无法保存指定的配置文件:"+fileName);

}

}



/**

* 保存配置文件,指定文件名

* @param fileName String

* @throws Exception

*/

public void saveFile(String fileName)

throws Exception {

saveFile(fileName,"");

}



/**

* 保存配置文件,采用原文件名

* @throws Exception

*/

public void saveFile() throws Exception {

if(fileName.length()==0)

throw new Exception

("需指定保存的配置文件名");

saveFile(fileName);

}

/**

* 删除一个属性

* @param value String

*/

public void deleteValue(String value){

p.remove(value);

}

/**

* main method for test

* @param args String[]

*/

public static void main(String[] args) {

String file = "f://p.properties";

PropertiesUtil pu = new PropertiesUtil(file);

pu.list();

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java-properties-utils-1.7.1.jar是一个Java开发工具包,用于简化处理Java属性文件的操作。属性文件通常用于存储键值对,可以用于配置应用程序的参数。这个工具包提供了一些方便的方法,可以轻松地读取、写入和修改属性文件的内容。 该工具包提供了几个主要的功能。首先,它可以通过提供文件路径来加载属性文件,并将其转化为Java的属性对象。这样,我们就可以通过代码来读取和操作属性文件中的键值对。 其次,该工具包提供了一些实用的方法,用于读取和获取属性文件中的特定键值对。例如,我们可以使用getProperty()方法根据键来获取属性值,还可以使用containsKey()方法来检查属性文件中是否包含特定的键。 此外,该工具包还提供了一些方便的方法,用于向属性文件中写入和更新键值对。使用setProperty()方法可以设置属性值,而使用store()方法可以将更改后的属性文件写入到指定的文件路径中。 除了基本的读写操作,该工具包还提供了其他一些有用的功能。例如,它可以将属性对象转化为Map对象,方便我们在程序中进行进一步的处理。此外,它还支持将属性文件转化为XML格式,以便于在不同平台和环境中的数据传输和交换。 总之,java-properties-utils-1.7.1.jar是一个功能强大的Java开发工具包,可以方便地处理属性文件,并提供了许多方便的方法和功能,有助于简化我们在Java应用程序中对属性文件的操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值