当前位置:首页 » 编程语言 » javaproperties读取

javaproperties读取

发布时间: 2022-07-12 11:01:17

java如何读取.properties文件的数据

在prop包下建立LoadProp.java文件。 3.有很多方法来读取.properties文件,现将主要方法罗列出来: a.通过class的getResourceAsStream()方法来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } 一定要注意的是,class里的getResourceAsStream()方法里参数的类路径一定要在前面加上"/",否则会报错 b.使用class的getClassLoader()方法所得的ClassLoader的getResourceAsStream()来读取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } ClassLoader的getResourceAsStream()方法与Class的getResourceAsStream()方法有点区别,在这里一定不要在类路径前面加上"/",否则会报错,是不是很奇怪。 c.使用ResourceBundle来读取 package prop; import java.util.ResourceBundle; public class LoadProp { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config/a"); System.out.println(rb.getString("name")); System.out.println(rb.getString("age")); } } 注意,getBundle()方法里的参数,是baseName,不要把后缀名写出来,并且不要加"/"。 好了,这是读取.properties文件的几种主要方法,还有其他的方法,基本上都大同小异。

② java中怎么读取properties文件

(1)load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();

(2)store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();

③ java如何读取properties文件

../conf/config.properties
还有src是程序运行的根目录,建议把所有与程序相关的都放到src里.不要放在外面

④ java怎么读取properties文件

最常用读取properties文件的方法
InputStream
in
=
getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:
InputStream
ins
=
this.getClass().getResourceAsStream(

⑤ 请教java如何实时读取.properties文件中的配置项内容

Properties 类

static {
properties = new Properties();

InputStream in = null;
try {
in = ReadProperties.class.getResourceAsStream("/brochina-config.properties");//文件路径
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String value = properties.getProperty(key);具体方法

⑥ 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配置文件

最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用: InputStream ins = this.getClass().getResourceAsStream(

⑧ 在java中如何读取properties文件

最常用读取properties文件的方法
InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");
Java中获取路径方法
获取路径的一个简单实现
反射方式获取properties文件的三种方式

1 反射方式获取properties文件最常用方法以及思考:
Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但我见到众多读取properties文件的代码中,都会这么干:

InputStream in = getClass().getResourceAsStream("资源Name");

这里面有个问题,就是getClass()调用的时候默认省略了this!我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。

问题是:假如我不想让某个类有对象,那么我会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,我要在静态块或者静态方法中获取properties文件,这个方法就行不通了。

那怎么办呢?其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那还不容易啊--
取所有类的父类Object,用Object.class难道不比你的用你正在写类自身方便安全吗 ?呵呵,下面给出一个例子,以方便交流。
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

/**
* 读取Properties文件的例子
* File: TestProperties.java
* User: leimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
private static String param1;
private static String param2;

static {
Properties prop = new Properties();
InputStream in = Object. class .getResourceAsStream( "/test.properties" );
try {
prop.load(in);
param1 = prop.getProperty( "initYears1" ).trim();
param2 = prop.getProperty( "initYears2" ).trim();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 私有构造方法,不需要创建对象
*/
private TestProperties() {
}

public static String getParam1() {
return param1;
}

public static String getParam2() {
return param2;
}

public static void main(String args[]){
System.out.println(getParam1());
System.out.println(getParam2());
}
}

运行结果:

151
152
当然,把Object.class换成int.class照样行,呵呵,大家可以试试。

另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法
2 获取路径的方式:
File fileB = new File( this .getClass().getResource( "" ).getPath());

System. out .println( "fileB path: " + fileB);

2.2获取当前类所在的工程名:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span>
/**

*获取项目的相对路径下文件的绝对路径

*

* @param parentDir

*目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or

* bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径

* @param fileName

*文件名

* @return一个绝对路径

*/

public static String getPath(String parentDir, String fileName) {

String path = null;

String userdir = System.getProperty("user.dir");

String userdirName = new File(userdir).getName();

if (userdirName.equalsIgnoreCase("lib")

|| userdirName.equalsIgnoreCase("bin")) {

File newf = new File(userdir);

File newp = new File(newf.getParent());

if (fileName.trim().equals("")) {

path = newp.getPath() + File.separator + parentDir;

} else {

path = newp.getPath() + File.separator + parentDir

+ File.separator + fileName;

}

} else {

if (fileName.trim().equals("")) {

path = userdir + File.separator + parentDir;

} else {

path = userdir + File.separator + parentDir + File.separator

+ fileName;

}

}

return path;

}

4 利用反射的方式获取路径:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" );

InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" );

InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );

⑨ java读取properties文件

Java读取properties文件的方法比较多;
在最常用的读取properties文件的方式--->“通过java.lang.Class类的getResourceAsStream(String name) 方法来实现”;
代码:
InputStream in = getClass().getResourceAsStream("资源Name");

⑩ 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();

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:536
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:232
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:532
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726