---
title: "属性文件的几种读取方法"
author: "Jesse_Dht"
date: "2006-07-12T08:31:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2006-07-12T08:31:00Z"
id: 28352
url: "https://jesse-dht.iteye.com/blog/28352"
---

# 1、   说明&lt;o:p&gt;&lt;/o:p&gt;

在我们做项目过程中，总要涉及到针对属性文件的读取，把一些字符串常量定义在属性文件中，方便了我们平时针对系统的维护。现在把几种常用的属性文件的读取方式总结一下。

# 2、   读取方式&lt;o:p&gt;&lt;/o:p&gt;

1)通过ResourceBundle类

我们定义了dnrmMessages.properties这个属性文件：

PERSPECTIVE\_ID=com.jctx.dnrm.TrmsPerspective&lt;o:p&gt;&lt;/o:p&gt;

定义一个工具类文件**Message.java**

package com.jctx.dnrm;

&lt;o:p&gt; &lt;/o:p&gt;

import java.util.MissingResourceException;

import java.util.ResourceBundle;

&lt;o:p&gt; &lt;/o:p&gt;

public class Message {

//定义属性文件的路径&lt;o:p&gt;&lt;/o:p&gt;

       private static final String BUNDLE\_NAME = "com.jctx.dnrm.dnrmMessages";

       private static final ResourceBundle Resource\_Bundle = ResourceBundle.getBundle(BUNDLE\_NAME);

      

       private Message(){

       }

       public static String getString(String key){

              try{

                     return Resource\_Bundle.getString(key);

              }

              catch (MissingResourceException e){

                     return "!" + key + "!";

              }    

       }

}

如果要取属性文件中的值，则这样的读取：

Message.getString("PERSPECTIVE\_ID");

2)通过NLS

一般用于eclipse的插件开发，NLS为包org.eclipse.osgi.util下的一个文件。我们定义了一个消息类文件，继承于NLS类，如下所示：

import org.eclipse.osgi.util.NLS;

&lt;o:p&gt; &lt;/o:p&gt;

public class LogicMessages extends NLS

{

public static String AddCommand\_Label;

static {

    NLS.initializeMessages("org.eclipse.gef.examples.logicdesigner.messages",LogicMessages.class); //$NON-NLS-1$

}

}

属性文件messages.properties为：

AddCommand\_Label=Add command&lt;o:p&gt;&lt;/o:p&gt;

当我们需要读取属性文件值的时候，利用下面的方法便可获得对应的属性值：

LogicMessages.AddCommand\_Label&lt;o:p&gt;&lt;/o:p&gt;

3)通过Platform.getBundle()方法

当我们在做插件开发时，把有部分属性信息写道plugin.properties文件中。这时，我们需要读取plugin.properties里的信息。如果对应插件ID为com.jctx.trms,读取plugin.properties（中文系统对应的文件名位：plugin\_zh\_CN.properties）的属性文件方式如下:

package com.jctx.trms;

&lt;o:p&gt; &lt;/o:p&gt;

import java.util.MissingResourceException;

import org.eclipse.core.runtime.Platform;

import org.osgi.framework.Bundle;

&lt;o:p&gt; &lt;/o:p&gt;

public interface LogicMessages {

    static class Helper {

        public static String getString(String key) {

               Bundle bundle = Platform.getBundle("com.jctx.trms");

            try {

                return Platform.getResourceString(bundle,key);

            }

            catch (MissingResourceException e) {

                return key;

            }

        }

    }

}

在程序中可以通过以下方式获取属性key对应的值

LogicMessages.Helper.getString("%GraphicalEditor\_FILE\_DELETED\_TITLE");

4)将属性值定义到一个接口文件中去

package org.eclipse.ui.examples.rcp.browser;

&lt;o:p&gt; &lt;/o:p&gt;

public interface IBrowserConstants {

    public static final String BROWSER\_PERSPECTIVE\_ID = PLUGIN\_ID + ".browserPerspective"; //$NON-NLS-1$

    public static final String BROWSER\_VIEW\_ID = PLUGIN\_ID + ".browserView";

    public static final String HISTORY\_VIEW\_ID = PLUGIN\_ID + ".historyView";

    public static final String COMMAND\_PREFIX = PLUGIN\_ID + ".commands."; //$NON-NLS-1$

    public static final String PREF\_HOME\_PAGE = "homePage"; //$NON-NLS-1$

    public static final String MEMENTO\_URL = "url"; //$NON-NLS-1$

}

5)通过Properties类和InputStream

Properties pr = **new** Properties();&lt;o:p&gt;&lt;/o:p&gt;

InputStream input = SimpleAgent.class.getResourceAsStream("mib.Properties");&lt;o:p&gt;&lt;/o:p&gt;

pr.load(input)&lt;o:p&gt;&lt;/o:p&gt;

当读取属性文件时，用pr.getProperty("key").trim()即可获得所需要的value了。

以上总结了几种属性的读取方法，您如果还有其他好的方式，我们可以交流讨论一下。

&lt;o:p&gt;&lt;/o:p&gt;