㈠ 在java中如何读取properties文件
使用java.util.Propertiesx0dx0ax0dx0a1、创建一个Properties对象。x0dx0a2、使用对象的load方法加载你的property文件。x0dx0a3、使用getProperty方法取值。x0dx0a例子:x0dx0apackage com.bill.test;x0dx0ax0dx0aimport java.io.FileInputStream;x0dx0aimport java.util.Properties;x0dx0ax0dx0apublic class Test {x0dx0apublic static void main(String[] args) throws Exception{x0dx0aProperties property = new Properties();x0dx0aproperty.load(new FileInputStream("你的文件位置"));x0dx0aString value = property.getProperty("你的属性的key");x0dx0a//TODO 使用value...x0dx0a}x0dx0a}
㈡ java读取properties文件
InputStream in = getProperties.class.getClassLoader().getResourceAsStream(
"config.properties");
这一句换个写法试试:
Properties props = new Properties();
String url = this.getClass().getClassLoader().getResource(
"config.properties").toString().substring(6);
String empUrl = url.replace("%20", " ");// 如果你的文件路径中包含空格,是必定会报错的
System.out.println(empUrl);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(empUrl));
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
我就是一直这么写的,没问题。
我猜你读取文件为空的原因,是你的文件路径有空格。
㈢ java中如何取得properties文件的路径
首先你为什么要重命名src,这个是放源码的包,另外你之所以取不到是因为你当前路径(classpath)下没有src啊,src是上一层,你肯定找不到,
㈣ JAVA如何读取指定路径的properties文件 不是在工程下的
public static Properties p =null;
static{
p =System.getProperties() ;
try {
p.load(new FileInputStream(new File("D:\\PIC\\srv_cfg.properties")));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(p.getProperty("common.config"));
}
㈤ java读取properties配置文件路径问题
说说我的项目中的情况吧:
配置文件“weblogic11g.properties”保存在WEB-INFO目录下,和web.xml在同一个目录下。
一个JavaBean专门用于读取配置文件的内容:
public class PropertiesIO {
private String fileName = null;
public PropertiesIO(String fileName){
this.fileName = getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName;
}
public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(in);
in.close();
return prop.getProperty(key);
}
catch(Exception err){
err.printStackTrace();
return null;
}
}
}
重点说明:getClass().getClassLoader().getResource("/")会得到当前项目下的“WEB-INF\classes”目录,即JavaBean的*.class文件的根目录,
getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName
就会得到当前项目下的“WEB-INF\weblogic11g.properties”文件。
getValue()是根据键值得到相应配置项的内容,这样就简单了。
㈥ java怎么读取properties文件
/*** 用来测试获取配置文件的类* @author BaiKeyang**/public class ResourceUtils {public static String getValue(String file, String key) { String value; // 未取到值,从properites文件中查找 try { ResourceBundle bundle = ResourceBundle.getBundle(file); value = bundle.getString(key); if (value==null){ throw new RuntimeException("没有该属性的值:"+key); } value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); return value; } catch (Throwable e) { System.err.println(e); return null; } }public static void main(String[] args) {String resourceFile = "config.dataBase";// 创建一个默认的ResourceBundle对象 // ResourceBundle会查找包config下的dataBase.properties的文件 // config是资源的包名,它跟普通java类的命名规则完全一样: // - 区分大小写 // - 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样 // - 资源文件必须位于指定包的路径之下(位于所指定的classpath中) // 假如你是在非Web项目中使用,则一定要写资源文件的路径,也就是包路径必须存在。// 如果是Web项目,不写包路径可以,此时将资源文件放在WEB-INF\classes\目录下就可以。String url = getValue(resourceFile, "db.url");System.out.println(url);}}
㈦ java web工程中读取properties文件,路径一直不知道怎么写
InputStreamin=getClass().getResourceAsStream("/config.properties");
在静态方法中,由于不能使用getClass()方法,必须写出类的名字。区别不大。
MyClass.class.getResourceAsStream("/config.properties");
使用这个方法,路径前面可以加斜杠也可以不加。根据Class类getResourceAsStream()方法的JavaDoc:
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').
就是说,这个path假如以斜杠开头,则斜杠后面的部分是文件相对classpath的路径;假如不是,Java会把这个path看作是“包名/文件名”的结构,会尝试在这个类的包里面去找,而不是从classpath开始找;在这种情况下,除非你把properties文件放到MyClass.class所属的包里面,不然都会是null的。
MyClass.class.getClassLoader().getResourceAsStream("config.properties");
这是因为使用classloader进行读取,所输入的参数必须是一个相对classpath的绝对路径,在格式上,一个绝对路径是不能以'/'开头的。
注意这两个方法是同名的,但路径参数的格式截然不同。
现在几乎所有的web project都是maven project,Maven的默认设置是把
src/main/resources/
加入到classpath里面的。那么,最好的做法是把你的properties文件放进src/main/resources里面,然后用上面代码读取。用Class类的,一般要加斜杠;用ClassLoader类的,绝不能加斜杠!
假如是Eclipse里面,需要把这个src/main/resources加到classpath里面。具体操作是右击工程,选择“Configure buildpath”,根据Maven的要求,把src/main/java和src/main/resources都加进去,并且保证Exclude是none,Include是all,或者至少要包括你需要读取的文件。
㈧ JAVA中如何读取src下所有的properties文件
1.使用java.util.Properties类的load()方法
示例:
//文件在项目下。不是在包下!!
InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;
Properties p = new Properties();
p.load(in) ;
String className2 = p.getProperty("database.driver");
String url = p.getProperty("database.url");
String user = p.getProperty("database.user");
String password = p.getProperty("database.pass");
总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(path);
InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));
Properties prop = new Properties();
㈨ java中怎么读取properties
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTest
{
/*定义静态方法,类名可以直接调用,用于读取properties属性文件中的内容*/
public static void initFile()
{
/*D://a.properties源属性文件的路径和名字*/
File file = new File("D://a.properties");
FileInputStream fis = null;
try
{
/*输入流和属性文件关联*/
fis = new FileInputStream(file);
/*创建属性集对象*/
Properties prop = new Properties();
/*将读取的内容加载到属性集对象中*/
prop.load(fis);
/*返回属性列表中所有键的枚举*/
Enumeration enums = prop.propertyNames();
while (enums.hasMoreElements())
{
/*将每一条属性强制转换为String类型,得到键key*/
String key = (String) enums.nextElement();
/*根据键得到对应的值value(String类型)*/
String value = prop.getProperty(key);
/*输出properties属性文件的内容*/
System.out.println(key+"----"+value);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
/*调用方法读取属性文件中内容*/
PropertiesTest.initFile();
}
}
结果是:
UserPass----
URl----jdbc:microsoft:sqlserver://localhost:1433;databasename=employee
Driver----com.microsoft.jdbc.sqlserver.SQLServerDriver
UserName----sa
㈩ java读取properties文件
个人建议使用FileInputStrean流 你试试看。因为文件流加载文件应该没问题。
InputStream in = getProperties.class.getClassLoad().getResourceAsStream(
"config.properties");这句改成FileInputStream in=new FileInputStream("/config.properties")看看。