我写了一个读配置文件的类,如下:
import java.io.*;
import java.util.*;/**
* @author Administrator
* 读取配置文件的相关属性。
*
*
*/
public class GetCfgProperty {
public GetCfgProperty(){
}
/**
* 得到配置文件的相关属性
* @param FileName 文件名
* @param PropertyName 属性名
* @return
*/
public String getProperty(String FileName,String PropertyName){
Properties fp = new Properties();
String PropertyValue="";
try{
fp.load(new FileInputStream(FileName)); //从文件读取信息
PropertyValue=fp.getProperty(PropertyName);
}catch(FileNotFoundException e){
System.out.println("文件"+FileName+"不存在");
}catch(IOException e){
e.printStackTrace();
}
return PropertyValue;
} public static void main(String[] args) {
GetCfgProperty test=new GetCfgProperty();
String OutPut="";
String OutPut2="";
OutPut=test.getProperty("text.txt","testProperty");
OutPut2=test.getProperty("text.txt","testProperty2");
System.out.println(OutPut+"\n");
System.out.println(OutPut2);
}
}
但不知道怎么配置文件存放的路径,望各位给点意见.
import java.io.*;
import java.util.*;/**
* @author Administrator
* 读取配置文件的相关属性。
*
*
*/
public class GetCfgProperty {
public GetCfgProperty(){
}
/**
* 得到配置文件的相关属性
* @param FileName 文件名
* @param PropertyName 属性名
* @return
*/
public String getProperty(String FileName,String PropertyName){
Properties fp = new Properties();
String PropertyValue="";
try{
fp.load(new FileInputStream(FileName)); //从文件读取信息
PropertyValue=fp.getProperty(PropertyName);
}catch(FileNotFoundException e){
System.out.println("文件"+FileName+"不存在");
}catch(IOException e){
e.printStackTrace();
}
return PropertyValue;
} public static void main(String[] args) {
GetCfgProperty test=new GetCfgProperty();
String OutPut="";
String OutPut2="";
OutPut=test.getProperty("text.txt","testProperty");
OutPut2=test.getProperty("text.txt","testProperty2");
System.out.println(OutPut+"\n");
System.out.println(OutPut2);
}
}
但不知道怎么配置文件存放的路径,望各位给点意见.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货