分享java读写Properties文件

Properties用来做配置之类的文件存储,比如数据库配置,连接池配置等。

转载请注明出处:分享java读写Properties文件


[java]  view plain copy
  1. package com.zuidaima;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.Properties;  
  8.   
  9. public class Main {  
  10.   
  11.     public static void main(String[] args) {  
  12.         Properties property = new Properties();  
  13.         try {  
  14.             File file = new File("c:/db.properties");  
  15.             if (!file.exists()) {  
  16.                 file.createNewFile();  
  17.             }  
  18.             // 写入  
  19.             property.setProperty("database""localhost");  
  20.             property.setProperty("user""zuidaima");  
  21.             property.setProperty("password""password");  
  22.             property.store(new FileOutputStream(file), null);  
  23.   
  24.             property.load(new FileInputStream(file));  
  25.   
  26.             // 读取  
  27.             System.out.println(property.getProperty("database"));  
  28.             System.out.println(property.getProperty("user"));  
  29.             System.out.println(property.getProperty("password"));  
  30.         } catch (IOException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34. }  

代码下载地址: http://www.zuidaima.com/share/1550463235050496.htm

你可能感兴趣的:(java,读写properties)