⑴ java怎么读取properties文件
利用java.util.Properties类进行操作
一、步骤如下:
0、创建Properties类对象
1、取得properties文件的输入流
2、使用Properties类加载该输入流内容
3、关闭输入流节约资源
4、此时可以直接操作Properties对象取得文件中的内容了
二、Properties类说明
0、是Hashtable的子类,所以具有Hashtable的性质
1、可以通过load方法加载输入流
2、具有特有的查询方法,可以通过getProperties查询某个键的值或propertyNames查询所有键枚举或stringPropertyNames查询所有键集
3、 添加属性具有特定的方法,可以通过setProperty添加(同put方法)
4、属性内容加载到打印输出流,可以通过list方法
5、属性保存到文件,可以通过store保存为properties和storeToXML方法保存为xml文件
三、实例程序
给出了对属性文件增删改查及保存的方法
⑵ 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();
⑶ property在Java中的用法
JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法:
1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。
⑷ 在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文件内容不再是难题
在项目开发中,处理properties文件内容读取问题,尤其是需要动态修改变量值,不再修改代码时,选择合适的方法至关重要。本文通过Spring+SpringMVC+Mybatis整合开发项目,详细梳理了五种实现方式,旨在解决这一常见需求,以供开发者参考。
### 五种实现方式详解
#### 1. **通过context:property-placeholder加载配置文件
使用``加载配置文件,简化配置。
注意:在`spring-mvc.xml`文件中进行配置时,确保使用`context:component-scan`标签,并设置`use-default-filters="false"`,以避免不必要的加载。
#### 2. **使用注解注入
在代码中使用`@Value`注解注入properties文件中的值。
配置文件路径:`classpath:jdbc.properties`
#### 3. **利用`util:properties`标签
使用``暴露properties文件内容。
在`spring-.xml`文件头部声明``。
#### 4. **自定义PropertyPlaceholderConfigurer
创建`PropertyConfigurer`类,继承`PropertyPlaceholderConfigurer`,覆盖`processProperties`方法并保存配置信息。
配置文件路径:`classpath:jdbc.properties`
使用方式:在需要使用该配置信息的类中注入`PropertyConfigurer`实例。
#### 5. **自定义工具类PropertyUtil
创建`PropertyUtil`类,静态代码块读取properties文件内容保存到静态属性中,供其他程序调用。
配置文件路径:`jdbc.properties`
通过`getProperty`方法获取文件内容,`getProperty`方法支持默认值。
### **注意事项及建议**
- 前三种方式相对死板,适用于特定场景,需要在配置文件中声明使用。
- 建议使用第四种和第五种配置方式,第五种方式最佳,无需额外注入,静态方法直接调用,高效且方便。
### **测试验证**
- 创建`PropertiesService`接口及其实现类`PropertiesServiceImpl`,通过`PropertyController`控制器类测试不同方式的实现效果。
- 测试代码覆盖所有实现方式,验证其可用性。
### **总结**
通过本文介绍的五种方法,开发者可以根据项目需求灵活选择,优化代码结构,提高开发效率。同时,理解`context:component-scan`标签的`use-default-filters`属性及原理,有助于更高效地定位和解决问题。加入Java架构开发群,获取更多学习资源和交流机会,不断精进技能。
⑹ 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();
}
我就是一直这么写的,没问题。
我猜你读取文件为空的原因,是你的文件路径有空格。