Properties的相对路径以及⽂件的读取操作
在我们平时写程序的时候,有些参数是经常改变的,⽽这种改变不是我们预知的。⽐如说我们开发了⼀个操作数据库的模块,在开发的时候我们连接本地的数据库那么 IP ,数据库名称,表名称,数据库主机等信息是我们本地的,要使得这个操作数据的模块具有通⽤性,那么以上信息就不能写死在程序⾥。通常我们的做法是⽤配置⽂件来解决。
各种语⾔都有⾃⼰所⽀持的配置⽂件类型。⽐如 Python,他⽀持 .ini⽂件。因为他内部有⼀个 ConfigParser类来⽀持 .ini⽂件的读写,根据该类提供的⽅法程序员可以⾃由的来操作 .ini⽂件。⽽在 Java中, Java⽀持的是 .properties⽂件的读写。 JDK内置的 java.util.Properties类为我们操作 .properties⽂件提供了便利。
⼀. .properties⽂件的形式 ==========================================================
#以下为服务器、数据库信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
#以下为数据库表信息
dbTable = mytable
#以下为服务器信息
ip = 192.168.0.9
······
在上⾯的⽂件中我们假设该⽂件名为: test.properties⽂件。其中 #开始的⼀⾏为注释信息;在等号“ =”左边的我们称之为 key;等号“ =”右边的我们称之为 value 。(其实就是我们常说的键 -值对) key应该是我们程序中的变量。⽽ value是我们根据实际情况配置的。
⼆. JDK中的 Properties类 Properties类存在于胞 Java.util中,该类继承⾃ Hashtable ,它提供了⼏个主要的⽅法:1. (  key) ,⽤指定的键在此属性列表中搜索属性。也就是通过参数 key,得到 key所对应的 value。
2. (  inStream) ,从输⼊流中读取属性列表(键和元素对)。通过对指定的⽂件(⽐如说上⾯的 test.properties⽂件)进⾏装载来获取该⽂件中的所有键 -值对。以供(  key) 来搜索。3. (  key,  value) ,调⽤ Hashtable 的⽅法 put 。他通过调⽤基类的put⽅法来设置键 -值对。
4. (  out,  comments) , 以适合使⽤ ⽅法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写⼊输出流。与 load⽅法相反,该⽅法将键 -值对写⼊到指定的⽂件中去。
5. (),清除所有装载的键-值对。该⽅法在基类中提供。
有了以上⼏个⽅法我们就可以对 .properties⽂件进⾏操作了!
简单实例:
package cn.yansmon.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;error 1962:no operating
import java.util.Properties;
/**
* 读取properties⽂件
*
* @author yans
*
*/
public class Configuration {
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;
/**
* 初始化Configuration类
*/
public Configuration() {
propertie = new Properties();
}
/**
* 初始化Configuration类
*
* @param filePath
*            要读取的配置⽂件的路径+名称
*/
public Configuration(String filePath) {
propertie = new Properties();
try {
inputFile = new Class().getClassLoader()
.getResource(filePath).getPath());
propertie.load(inputFile);
一个线程崩溃会引起整个进程崩溃inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性⽂件--->失败!- 原因:⽂件路径错误或者⽂件不存在");  ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载⽂件--->失败!");
ex.printStackTrace();
}
}// end ReadConfigInfo(...)
/**
* 重载函数,得到key的值
*
* @param key
*            取得其值的键
* @return key的值
*/java是什么意思和c语言有什么区别
public String getValue(String key) {
if (ainsKey(key)) {
String value = Property(key);// 得到某⼀属性的值
return value;
} else
return "";
}// end getValue(...)
/**
* 重载函数,得到key的值
*
* @param fileName
*            properties⽂件的路径+⽂件名
* @param key
*            取得其值的键
* @return key的值
*/
public String getValue(String fileName, String key) {
try {
String value = "";
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();
if (ainsKey(key)) {其密级()进行标识
value = Property(key);
return value;
} else
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();properties文件用什么打开
return "";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}// end getValue(...)
/**
* 清除properties⽂件中所有的key和其值
*/
public void clear() {
propertie.clear();
}// end clear();
/**
* 改变或添加⼀个key的值,当key存在于properties⽂件中时该key的值被value所代替,当key不存在时,该key的值是value  *
* @param key
*            要存⼊的键
* @param value
*            要存⼊的值
*/
public void setValue(String key, String value) {
propertie.setProperty(key, value);
}// end setValue(...)
/**
* 将更改后的⽂件数据存⼊指定的⽂件中,该⽂件可以事先不存在。
*
* @param fileName
*            ⽂件路径+⽂件名称
* @param description
*            对该⽂件的描述
*/
public void saveFile(String fileName, String description) {
try {
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}// end saveFile(...)
public static void main(String[] args) throws IOException {
Configuration rc = new Configuration("powers.properties");
String[] powerList = rc.getValue("list").split(",");
for (String po : powerList) {
System.out.println(po);
}
}
}
路径相关问题:
在java中使⽤相对路径
  ⽆标题⽂档
  ?
  在开发过程中,我们经常会遇到读取配置⽂件的情况,对于配置⽂件的读取,根据环境等情况⼜各有不同,⼀般情况下,如果从⾮jar包中使⽤相对/路径,⽐较简单,就不在累述了,⽽在很多
  情况下,我们需要把我们的class打包成jar⽂件,进⾏使⽤,这时就会发现,我们先前如果没有考虑到这些,可能就⾏不通了,那么,该如何解决呢?⽅法如下
  :
  有如下路径 :
  Web-info--|-->classes--->conf-->config.properties
  |-->lib
  此时加⼊我们需要读取config.properties,在不使⽤jar包时,使⽤如下⽅式读取,不失为⼀种⽅法:
  File f = new Class().getResource("/").getPath());
  f = new Path() + "/conf/config.properties");
或者:(Class().getClassLoader().getResource(fileName).getPath())
  注:f.getPath()即为当class所在的绝对路径。如:c:\javasrc\web-inf\classes
  然后,对⽂件对象进⾏处理,就能把配置信息读取出来了,但是加⼊如上class被打包成jar⽂件,那么,在程序执⾏到这⾥时,就会⽆法到配置⽂件,那么该如何处理呢?
  处理⽅法如下:
  String s_config="conf/config.properties";
  InputStream in = SystemResourceAsStream(s_config);
  if( in == null ){
  System.out.println( " 打开 " + s_config + "失败!" );
  }else
  {
  Properties properties = new Properties();
  properties.load(in);
  //
  //接下来就可以通过Property(String obj)⽅法对进⾏配置信息读取了
1. Property ("user.dir" )当前⽤户⽬录的相对路径。
2. Class().getResource("") 到的也是当前ClassPath 的绝对URI 路径。
3. SystemResource("") 到的也是当前ClassPath 的绝对URI 路径。
4. Thread.currentThread().getContextClassLoader().getResource("") 到的也是当前ClassPath 的绝对URI 路径。
读取Properties ⽂件的⽅法
1.使⽤java.util.Properties 类的load() ⽅法
⽰例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
gulp和webpack区别p.load(in);
2. 使⽤java.util.ResourceBundle 类的getBundle() ⽅法
⽰例: ResourceBundle rb = Bundle(name, Default());
3. 使⽤java.util.PropertyResourceBundle 类的构造函数
⽰例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4. 使⽤class 变量的getResourceAsStream() ⽅法
⽰例: InputStream in = ResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5. 使⽤ClassLoader() 所得到的java.lang.ClassLoader 的getResourceAsStream() ⽅法。
⽰例:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。