`
sunxboy
  • 浏览: 2869485 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Properties files example

阅读更多
package simple.file;

import java.util.Properties;
import org.junit.Test;

/**
 * 属性文件操作类
 * @author Simple Rong
 *
 */
public class PropFileHandle {

        /**
         * 获取classpath下的XML属性文件和Properties属性文件
         * @throws Exception
         */
        @Test
        public void testGetProps1() throws Exception {
                Properties props = new Properties();
//              props.loadFromXML(PropFileHandle.class.getResourceAsStream("/test.xml"));       //由本类来加载classpath下的XML属性文件
                props.load(PropFileHandle.class.getClassLoader().getResourceAsStream("test.properties"));       //由本类来加载classpath下的.properties属性文件
                String username = props.getProperty("username");        //找不到则返回null
                String password = props.getProperty("password");        //找不到则返回null
//              String username = props.getProperty("username","sa");   //找不到则返回默认值sa
//              String password = props.getProperty("password","sa");   //找不到则返回默认值sa
//              Assert.assertEquals(username, "simple");
//              Assert.assertEquals(password, "rong");
                System.out.println(username);
                System.out.println(password);
        }
        
        
        
        
}
 
分享到:
评论

相关推荐

    多种方式读取Properties代码示例

    public class PropertiesExample { public static void main(String[] args) { Properties prop = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream("config.properties"); /...

    Java读取Properties文件的六种方法.txt

    URL url = new URL("http://example.com/config.properties"); try (InputStream in = url.openConnection().getInputStream()) { props.load(in); } } catch (IOException e) { e.printStackTrace(); } ``` #...

    struts2的struts.properties介绍

    #### struts.configuration.files **含义**:这是一个配置文件列表,这些文件会自动被Struts2加载。 **示例**:可以通过此属性添加额外的配置文件,如`struts-default.xml`、`struts.xml`等,以便于组织和管理配置...

    struts2的struts.properties配置文件详解

    - **示例**:可以设置为 `customProperties` 或者 `com.example.properties` 等。 - **作用**:允许开发者定义自定义的配置属性文件,这些文件可以包含特定于项目的配置信息。 #### 8. `struts.devMode` - **描述*...

    SystemVerilog Assertions - Design Tricks and SVA Bind Files_pres

    SVA bind files are a mechanism for associating SVA properties with RTL code. This association enables the properties to be checked during simulation and formal verification without modifying the RTL ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Using Properties Files Section 11.2. Internationalizing Applications Section 11.3. Laying Out Pages with Tiles Section 11.4. Using Tiles Definitions Chapter 12. The Struts Framework: Validating ...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Using Properties Files Section 11.2. Internationalizing Applications Section 11.3. Laying Out Pages with Tiles Section 11.4. Using Tiles Definitions Chapter 12. The Struts Framework: Validating ...

    LMD.StoragePack.v1.06.30.For.Delphi

    either by using example component or own methods Allows encoding/decoding of simple data types: String & Integer (e.g. for password data) Supports Autoload & Autosave of component properties ...

    ros by example for indigo volume 2

    3.1.2 Key properties of behavior trees..............................................................................57 3.9.3 Building a behavior tree......................................................

    Professional C# 3rd Edition

    Attributes and Properties 461 Communication between Contexts 462 Remote Objects, Clients, and Servers 462 Remote Objects 462 A Simple Server 464 A Simple Client 465 .NET Remoting Architecture 466 ...

    Silverlight多文件(大文件)上传项目源码

    - Configure file extension filter (for example, only .jpg files) (配置支持的格式,如仅仅支持jpg) - Configure maximum file size (设置最大上传尺寸) - Configure the number of simultaneous uploads (设置...

    实现邮件发送,包含jar包

    Files.readAllBytes(Paths.get("path_to_your_image.jpg")), "image/jpeg"))); imagePart.setHeader("Content-ID", "<image1>"); multipart.addBodyPart(imagePart); ``` 最后,使用`Transport.send(message)`方法...

    Windows7系统IIS7+Tomcat7集成.pdf

    "worker_mount_file"="D:\\Program Files\\Tomcat7\\conf\\uriworkermap.properties" "log.level"="realease" ``` - 双击此文件将其导入到系统注册表中。 ##### 4. 建立网站的虚拟目录 - 打开 Internet 信息...

    javamail收发邮件(带附件,正文带图).docx

    private Properties properties = new Properties(); public JavaMailboxAttachment() { try { properties.put("mail.smtp.host", mailHost); properties.put("mail.smtp.port", mailPort); properties.put(...

    IIS5和Tomcat6整合.doc

    - 在`C:\Program Files\Apache Software Foundation\Tomcat 6.0\conf`目录下创建`workers2.properties`和`jk2.properties`两个文件。 - `workers2.properties`文件内容示例: ```properties [shm] # file 的值...

    VB编程资源大全(英文源码 文件)

    syscolor.zip This is an example of how to retrieve the system colors and display them on your form.<END><br>13 , findfiles.zip This is an example of how to return all of the files on a users ...

    UE(官方下载)

    For example, If ^(h*o^) ^(f*s^) matches "hello folks", ^2 ^1 would replace it with "folks hello". Perl compatible regular expressions An introduction to using Perl-style regular expressions for ...

    IOS5 Programming Cookbook

    Projects typically consist of files such as source code files (.m), interface definition files (.h), resource files (images, sounds, etc.), and project settings files (.xcodeproj). ### 2. ...

    java_programming_manual_log_files.rar_java programming_日志

    Log4j的配置通常通过一个XML或.properties文件完成,如log4j.properties或log4j.xml。在这些文件中,你可以定义Loggers的级别、添加Appenders及其属性,并设置Layouts。例如: ```xml <logger name="com....

    DWGdirectX2.7.3

    For example, when saving a 2007 DWG file back to R14, 2007-specific properties are saved to the R14 file as xdata and then restored if the file is loaded back into a 2007-compatible application....

Global site tag (gtag.js) - Google Analytics