JAVA读写资源文件
来源:互联网 发布:灯具安装网络接单平台 编辑:程序博客网 时间:2024/04/25 14:11
1. 写入 properties 文件
设置属性值,然后把值写入属性文件: config.
import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Properties; public class App { public static void main(String[] args) { Properties prop = new Properties();OutputStream output = null; try { output = new FileOutputStream("config.properties"); // 设置属性值prop.setProperty("database", "localhost");prop.setProperty("dbuser", "mkyong");prop.setProperty("dbpassword", "password"); // 保存属性值prop.store(output, null); } catch (IOException io) {io.printStackTrace();} finally {if (output != null) {try {output.close();} catch (IOException e) {e.printStackTrace();}} } }}
2. 读入 properties 文件
从属性文件中把值载入系统
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties; public class App { public static void main(String[] args) { Properties prop = new Properties();InputStream input = null; try { input = new FileInputStream("config.properties"); // 载入属性文件prop.load(input); // 获取属性值打印System.out.println(prop.getProperty("database"));System.out.println(prop.getProperty("dbuser"));System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) {ex.printStackTrace();} finally {if (input != null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}} }}
3. 从项目类路径中载入属性文件
import java.io.IOException;import java.io.InputStream;import java.util.Properties; public class App { public static void main( String[] args ){ Properties prop = new Properties(); InputStream input = null; try { String filename = "config.properties"; input = App3.class.getClassLoader().getResourceAsStream(filename); if(input==null){ System.out.println("Sorry, unable to find " + filename); return; } //从类路径中载入属性文件 prop.load(input); //获取属性值打印 System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } finally{ if(input!=null){ try {input.close();} catch (IOException e) {e.printStackTrace();} } } }}
4. 打印 properties 文件的所有内容
import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.Properties; public class App { public static void main(String[] args) {App app = new App();app.printThemAll(); } private void printThemAll() { Properties prop = new Properties();InputStream input = null; try { String filename = "config.properties";input = getClass().getClassLoader().getResourceAsStream(filename);if (input == null) {System.out.println("Sorry, unable to find " + filename);return;} prop.load(input); Enumeration<?> e = prop.propertyNames();while (e.hasMoreElements()) {String key = (String) e.nextElement();String value = prop.getProperty(key);System.out.println("Key : " + key + ", Value : " + value);} } catch (IOException ex) {ex.printStackTrace();} finally {if (input != null) {try {input.close();} catch (IOException e) {e.printStackTrace();}}} } }
源自:http://www.mkyong.com/java/java-properties-file-examples/
0 0
- JAVA读写资源文件
- 资源文件读写
- Properties读写资源文件
- java小型数据存储,资源文件读写示例
- Java资源文件的读写操作(Properties类)
- C#下面读写资源文件
- delphi读写程序资源文件
- NDK读写assert资源文件
- Java杂谈之读写文件,复制图片以及下载网站链接资源
- android jni 读写资源文件(assets)
- 使用C#读写外部的资源文件
- C# 文件读写与内存资源泄漏
- android jni 读写资源文件:assets
- 记PE文件中读写资源
- Unity读写资源包里面的文件
- java 读写ini文件
- java读写文件
- Java读写文件方法
- java学习笔记<八>final修饰符
- GO和C++代码对比,GO少个25%代码
- 山理工acm各位数相加
- 第三周:接口
- 网络编程(7)字节序对跨平台数据传输的影响
- JAVA读写资源文件
- NVelocity介绍
- JAVA UUID 生成
- Rust运行时指南(官方文档翻译)
- 冒泡排序
- 6. redhat grep、egrep、fgrep
- 开源一个适用于双Y轴的iOS折线图控件
- Centos Fedora 编译R
- c++格式