---
title: "读取src下面的资源文件"
author: "wangguangji"
date: "2011-06-27T12:58:59Z"
tags: []
categories: ["编程语言"]
slug: "src"
draft: false
lastmod: "2011-06-27T12:58:59Z"
id: 1106276
url: "https://wangxingrang.iteye.com/blog/1106276"
---

在做开发的时候，我们会遇见到这种情况，就是我们要写一些资源文件，例如一些配置文件放在SRC根目录下面，一般放的是xml,properties这些格式的文件，在系统启动的时候就加载这些文件。

我们是通过怎样的一个方法获得这些文件呢？
  答案是这样的：因为这些配置文件都是放到src下面的，我们使用ClassLoader的getResourceAsStream这个方法可以加载这些资源文件
```java

InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("文件.xml");
```


要是.properties文件结尾的文件 我们在得到inputStream文件的时候可以使用
Properties properties = new Properties();
properties.load(in);
String value = properties.getProperty(name);
因为.properties文件就是文本文件里面放的都是一些键值对