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
原创粉丝点击