
1使用javautilProperties类的load()方法
示例:
//文件在项目下。不是在包下!!
InputStream in = new BufferedInputStream(new FileInputStream("demoproperties")) ;
Properties p = new Properties();
pload(in) ;
String className2 = pgetProperty("databasedriver");
String url = pgetProperty("databaseurl");
String user = pgetProperty("databaseuser");
String password = pgetProperty("databasepass");
2 使用javautilResourcebundle类的getbundle()方法
//前面没有“/”代表当前类的目录
示例:
//文件和类在同一个包下,注意它的文件名和后缀!!是调换的,
// 这里我也不知道为什么文件名和后缀名要调换?? 知道的麻烦您告诉我一声,谢谢!!
ResourceBundle resource = ResourceBundlegetBundle("propertiesjdbc");
String className = resourcegetString("databasedriver");
String url = resourcegetString("databaseurl");
String user = resourcegetString("databaseuser");
String password = resourcegetString("databasepass");
3使用javautilPropertyResourceBundle类的构造函数
示例:
// 文件在项目下 或者 src/demoproperties
// 在 src/demoproperties 写成 new FileInputStream("src/demoproperties")
InputStream in = new BufferedInputStream(new FileInputStream("demoproperties"));
ResourceBundle rb = new PropertyResourceBundle(in) ;
String className4 = rbgetString("databaseurl");
4使用class变量的getresourceasstream()方法
示例:
InputStream in =PropertiesclassgetResourceAsStream("/properties/jdbcproperties");
// 包点类名下的。
// 如果找不到带有该名称的资源,则返回 null
Properties p = new Properties();
pload(in);
Systemoutprintln(pgetProperty("databaseurl"));
5使用classgetclassloader()所得到的javalangclassloader的getresourceasstream()方法
// properties 文件 要放在src下面,否则找不到啊
示例:
InputStream in = 类名classgetClassLoader()getResourceAsStream("jdbcproperties");
Properties p = new Properties() ;
pload(in);
Systemoutprintln(pgetProperty("databasepass"));
6使用javalangclassloader类的getsystemresourceasstream()静态方法
示例:
// 同包名下
InputStream in = ClassLoadergetSystemResourceAsStream("properties/jdbcproperties");
Properties p = new Properties() ;
pload(in) ;
Systemoutprintln(pgetProperty("databaseuser"));
打开properties方法:
properties属性文件内容都是以键值对形式存在的,比如写一个叫testproperties的文件,打开后可以在里面写入:name=Tom
而在java类中需要new一个Properties类的对象,如下:
Properties properties = new Properties();
接下来需要获取testproperties的文件路径:
String path = ThreadcurrentThread()getContextClassLoader()getResource("testproperties")getPath();
然后加载该文件:
propertiesload(new FileInputStream(path));
最后你就可以get它的属性了:
String name_1=propertiesgetProperty("name");
这个name_1的值就是“TOM”了。
1、先讲一下怎么读取项目内的配置文件,properties文件,里面有两个键值对name:爬楼高手和age:37。
2、然后在JAVA代码中初始化PropertiesProperties pro =new Properties()。
3、然后调用load方法读取项目中的properties文件:proload(new InputStreamReaderObjectclass。
4最后使用getProperty方法根据key来获取对应的value值progetProperty("name")。
5、读取放在D盘的一个properties文件。
6、本地的properties文件同样需要配置字符集,不然会出现乱码。
7、和读取项目中文件一样,new一个Porperties对象,然后调用load方法,通过key获取对应的值就行了propertiesload(new InputStreamReader(new BufferedInputStream。
说说我的项目中的情况吧:
配置文件“weblogic11gproperties”保存在WEB-INFO目录下,和webxml在同一个目录下。
一个JavaBean专门用于读取配置文件的内容:
public class PropertiesIO {
private String fileName = null;
public PropertiesIO(String fileName){
thisfileName = getClass()getClassLoader()getResource("/")getPath() + "\\" + fileName;
}
public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
propload(in);
inclose();
return propgetProperty(key);
}
catch(Exception err){
errprintStackTrace();
return null;
}
}
}
重点说明:getClass()getClassLoader()getResource("/")会得到当前项目下的“WEB-INF\classes”目录,即JavaBean的class文件的根目录,
getClass()getClassLoader()getResource("/")getPath() + "\\" + fileName
就会得到当前项目下的“WEB-INF\weblogic11gproperties”文件。
getValue()是根据键值得到相应配置项的内容,这样就简单了。
public void getCsisUrl(){
Properties p = new Properties();
try{
FileInputStream in = new FileInputStream(ServletActionContextgetRequest()getRealPath("/WEB-INF/classes/demoproperties"));
pload(in);
inclose();
String csisUrl= pgetProperty("csisUrl");
//Systemoutprintln(csisUrl);
}catch(Exception e){
eprintStackTrace();
}
}
读取Properties文件六种方法
开发项目时,经常把一些参数存入Properties文件,以增加程序的灵活性。可以通过以下六种方法读取配置参数(注意:spring对properties的读取也有很好的集成):
1、使用javautilProperties类的load()方法 示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
pload(in);
2、使用javautilResourceBundle类的getBundle()方法 示例:
ResourceBundle rb = ResourceBundlegetBundle(name, LocalegetDefault());
3、使用javautilPropertyResourceBundle类的构造函数 示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4、使用class变量的getResourceAsStream()方法 示例:
InputStream in = JPropertiesclassgetResourceAsStream(name);
Properties p = new Properties(); pload(in);
5、使用classgetClassLoader()所得到的javalangClassLoader的getResourceAsStream()方法 示例:
InputStream in = JPropertiesclassgetClassLoader()getResourceAsStream(name);
Properties p = new Properties(); pload(in);
6、使用javalangClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStream in = ClassLoadergetSystemResourceAsStream(name);
Properties p = new Properties(); pload(in);
补充
Servlet中可以使用javaxservletServletContext的
getResourceAsStream()方法 示例:
InputStream in = contextgetResourceAsStream(path);
Properties p = new Properties(); pload(in);
以上就是关于java中properties的load方法读取的文件内容怎么写全部的内容,包括:java中properties的load方法读取的文件内容怎么写、AD的Properties怎么打开、properties文件怎么打开啊等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)