properties配置文件的简单使用

作为刚进入程序员领域的新新人,有很多技术不懂,在日常工作中遇到不懂的方面就只能求助度娘了。不定期写些博客,记录在工作遇到问题后自己学习的情况。


*.properties是一种配置文件,在java文件中使用它时要先导入java.util.Properties包。

--------------------------properties支持的格式主要为key-value and xml类型。----------------------------

key-value:

#配置
ip = 192.168.0.4
username =
#password =

有#开头的是注释,注释是什么用不用说了~

允许value为空,会被默认为null。

properties 实现map接口存储key-value格式数据,存储无序,要取出数据时用key方式get得到想要value,使用load可以直接映射成map。是最常用的一种格式。

xml格式:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
<comment><span style="font-family:Tahoma,Arial;font-size:12px;color:#434343;font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; background-color: rgb(242, 242, 242); display: inline ! important; float: none;">配置</span></comment>  
<entry key="ip">192.168.0.4</entry>  
<entry key="username"></entry>
<pre name="code" class="java"><--<entry key="username"></entry>-->
</properties>

 与上面key-value格式对应。 

xml格式有个好处,有中文时不会出现乱码,xml文件与系统编译无关。当然,这个我没试用过,是在度娘查到的资料看到的。

----------------------------------------------------------end----------------------------------------------------------------------

----------------------------------------------配置文件的读取使用------------------------------------------------------------

import java.util.Properties;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public final class template {

    public template() {
    }

    public static void main(final String[] args) throws IOException {

        FileOutputStream fos = null;
        InputStream is=null;
        String  ip="";
        final Properties properties = new Properties();
        final Resource resource = new ClassPathResource("spring/settings.properties");//这里用了resource方法,具体作用以后再记录~~
        is = resource.getInputStream();
        properties.load(is);
        ip= (String) properties.get("ip");
        System.out.println("ip="+ip);//输出到控制台

//或者
//        Properties properties = new Properties();  
//        FileInputStream fis = new FileInputStream("spring/settings.properties");  
//        properties.load(fis);  
//        properties.list(System.out);  //输出方法,输出到控制台console
//        System.out.println("ip="+properties.getProperty("ip"));  

//输出到文件
        try{
            PrintStream ps = new PrintStream(new File("d:\\demo.properties"));
           properties.list(ps);
        } 
        catch (IOException e) {
           e.printStackTrace();
          }

        is.close();

    }
}
---------------------xml配置文件的读取使用----------------------------------------------

public class templateXml {  
      public static void main(String args[]) throws Exception {  
        Properties properties = new Properties();  
        FileInputStream fis = new FileInputStream("templateProperties.xml");  
        properties.loadFromXML(fis);  
        properties.list(System.out);  
        System.out.println("\nip= " + properties.getProperty("ip"));  

        //输出到文件
        try{
             PrintStream fs = new PrintStream(new File("d:\\demo.xml"));
             properties.storeToXML(fs,"test");//---------------storeToXML(OutputStream,comment)
         } catch (IOException e) {
             e.printStackTrace();
         }
     }  
 }  

感觉还很多没记录清楚~



final方法
将方法声明为final,那就说明你已经知道这个方法提供的功能已经满足你要求,不需要进行扩展,并且也不允许任何从此类继承的类来覆写这个方法,但是继承仍然可以继承这个方法,也就是说可以直接使用。另外有一种被称为inline的机制,它会使你在调用final方法时,直接将方法主体插入到调用处,而不是进行例行的方法调用,例如保存断点,压栈等,这样可能会使你的程序效率有所提高,然而当你的方法主体非常庞大时,或你在多处调用此方法,那么你的调用主体代码便会迅速膨胀,可能反而会影响效率,所以你要慎用final进行方法定义。

final类
当你将final用于类身上时,你就需要仔细考虑,因为一个final类是无法被任何人继承的,那也就意味着此类在一个继承树中是一个叶子类,并且此类的设计已被认为很完美而不需要进行修改或扩展。对于final类中的成员,你可以定义其为final,也可以不是final。而对于方法,由于所属类为final的关系,自然也就成了final型的。你也可以明确的给final类中的方法加上一个final,但这显然没有意义。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值