使用J2SE API读取Properties文件的六种方法
1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
6。使用java.lang.ClassLoader类的getSystemResourceAsStrea
示例: InputStream in = ClassLoader.getSystemResourceAsStrea
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
完整的示例,可以参考附件文件
如何上传文件,谁知道请告诉以下。 只好把source都贴上来了
JProperties.java文件
package com.kindani;
//import javax.servlet.ServletContext;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class JProperties {
public final static int BY_PROPERTIES = 1;
public final static int BY_RESOURCEBUNDLE = 2;
public final static int BY_PROPERTYRESOURCEBUNDLE = 3;
public final static int BY_CLASS = 4;
public final static int BY_CLASSLOADER = 5;
public final static int BY_SYSTEM_CLASSLOADER = 6;
public final static Properties loadProperties(final String name, final int type) throws IOException {
}
// ---------------------------------------------- servlet used
// ---------------------------------------------- support class
public static class ResourceBundleAdapter extends Properties {
}
}
JPropertiesTest.java文件
package com.kindani.test;
import junit.framework.*;
import com.kindani.JProperties;
//import javax.servlet.ServletContext;
import java.util.Properties;
public class JPropertiesTest extends TestCase {
JProperties jProperties;
String key = "helloworld.title";
String value = "Hello World!";
public void testLoadProperties() throws Exception {
}
}
properties文件与JPropertiesTest.java文件相同的目录下
LocalStrings.properties文件
# $Id: LocalStrings.properties,v 1.1 2000/08/17 00:57:52 horwat Exp $
# Default localized resources for example servlets
# This locale is en_US
helloworld.title=Hello World!
requestinfo.title=Request Information Example
requestinfo.label.method=Method:
requestinfo.label.requesturi=Request URI:
requestinfo.label.protocol=Protocol:
requestinfo.label.pathinfo=Path Info:
requestinfo.label.remoteaddr=Remote Address:
requestheader.title=Request Header Example
requestparams.title=Request Parameters Example
requestparams.params-in-req=Parameters in this request:
requestparams.no-params=No Parameters, Please enter some
requestparams.firstname=First Name:
requestparams.lastname=Last Name:
cookies.title=Cookies Example
cookies.cookies=Your browser is sending the following cookies:
cookies.no-cookies=Your browser isn't sending any cookies
cookies.make-cookie=Create a cookie to send to your browser
cookies.name=Name:
cookies.value=Value:
cookies.set=You just sent the following cookie to your browser:
sessions.title=Sessions Example
sessions.id=Session ID:
sessions.created=Created:
sessions.lastaccessed=Last Accessed:
sessions.data=The following data is in your session:
sessions.adddata=Add data to your session
sessions.dataname=Name of Session Attribute:
sessions.datavalue=Value of Session Attribute:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java对properties配置文件的操作
package com.yorsun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
public class PropertiesUnit {
private String filename;
private Properties p;
private FileInputStream in;
private FileOutputStream out;
public PropertiesUnit(String filename) {
}
public static String getConfigFile(HttpServlet hs) {
}
private static String getConfigFile(HttpServlet hs, String configFileName) {
}
public void list() {
}
public String getValue(String itemName) {
}
public String getValue(String itemName, String defaultValue) {
}
public void setValue(String itemName, String value) {
}
public void saveFile(String filename, String description) throws Exception {
}
public void saveFile(String filename) throws Exception{
}
public void saveFile() throws Exception{
}
public void deleteValue(String value){
}
public static void main(String args[]){
//
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.test.TestClass;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadPropertiesFile ...{
}
--------------------------------------------------------------------------------------------------------------------------------------
//=================sprin配置文件================================
<bean
</bean>
//=================java文件================================
package com.thtf.ezone.ezesb.jmx.admin.service.impl;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class UserServiceImpl implements UserService {
String filePath = null;
public void setFilePath(String filePath) {
}
public static void main(String dd[])throws Exception{
Properties p = new Properties();
FileInputStream ferr=new FileInputStream((getClass().getClassLoader()
p.load(ferr);
ferr.close();
Set s = p.keySet();
Iterator it = s.iterator();
while(it.hasNext()){
String id = (String)it.next();
String value = p.getProperty(id);
System.out.println(id+":="+value);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
//==============databaseconfing.properties 文件=====================
#----------------------------------
# sql server 2000数据厍连接信息
#----------------------------------
dp.sd.DataBaseTyp=mssql
DataBaseHost=127.0.0.1:1433
DataBase=formpro
UserName=sa
PassWord=01
#----------------------------------
# mysql 数据厍连接信息
#----------------------------------
#DataBaseHost=127.0.0.1:3306
#DataBaseTyp=mysql
#DataBase=snow
#UserName=root
#PassWord=01
//==========================运行结果=======================
PassWord:=01
DataBaseHost:=127.0.0.1:1433
DataBase:=formpro
dp.sd.DataBaseTyp:=mssql
UserName:=sa