按照下面的例子修改:
InputStream is = getClass().getResourceAsStream("dbjndi.properties");
Properties dbProps = new Properties();
try{
dbProps.load(is); String ipClasses = dbProps.getProperty("ip");
StringTokenizer st = new StringTokenizer(ipClasses); while (st.hasMoreElements()){
String ipClassName = st.nextToken().trim();
try{
this.ip = dbProps.getProperty("ip");
// System.out.println(this.ip);
}
catch(Exception e){
System.err.println("配置文件中不存在ip地址,请修改配置文件。");
}
}
String jndiClasses = dbProps.getProperty("dbjndi");
StringTokenizer stjndi = new StringTokenizer(jndiClasses); while (stjndi.hasMoreElements()){
String jndiClassName = stjndi.nextToken().trim();
try{
this.dbjndi = dbProps.getProperty("dbjndi");
// System.out.println(this.dbjndi );
}
catch(Exception e){
System.err.println("配置文件中不存在dbjndi名,请修改配置文件。");
}
}
}
catch(Exception e){
System.err.println("不能读取属性文件. 请确保dbjndi.properties在CLASSPATH指定的路径中");
return;
}
InputStream is = getClass().getResourceAsStream("dbjndi.properties");
Properties dbProps = new Properties();
try{
dbProps.load(is); String ipClasses = dbProps.getProperty("ip");
StringTokenizer st = new StringTokenizer(ipClasses); while (st.hasMoreElements()){
String ipClassName = st.nextToken().trim();
try{
this.ip = dbProps.getProperty("ip");
// System.out.println(this.ip);
}
catch(Exception e){
System.err.println("配置文件中不存在ip地址,请修改配置文件。");
}
}
String jndiClasses = dbProps.getProperty("dbjndi");
StringTokenizer stjndi = new StringTokenizer(jndiClasses); while (stjndi.hasMoreElements()){
String jndiClassName = stjndi.nextToken().trim();
try{
this.dbjndi = dbProps.getProperty("dbjndi");
// System.out.println(this.dbjndi );
}
catch(Exception e){
System.err.println("配置文件中不存在dbjndi名,请修改配置文件。");
}
}
}
catch(Exception e){
System.err.println("不能读取属性文件. 请确保dbjndi.properties在CLASSPATH指定的路径中");
return;
}
一直做的JSP ,对读写文件不熟!!
请多指教!!
从文本文件读或写
其中一行的一个变量的值,用到哪些类??
如何做呢?
思想:
把所有文件内容读出来,然后比较字符串(DELCARE=0),replace字符串,再write进文件!
能说说吗?
data.load(new FileInputStream("你的文件名"));
data.getProperty("id");这个方法可以取得任何 name=value 的文件的值
在UserJavaPerson中提到的
InputStream is = getClass().getResourceAsStream("dbjndi.properties");
方法中getResourceAsStream()方法中的参数的类型是否有限定(我的意思是说文件类型)谢谢
import java.util.Properties;
import java.util.Enumeration;
import java.util.logging.*;public class ReadPropertyFile {
public static void main(String[] args) {
String pFilename = System.getProperty("user.dir")
+ System.getProperty("file.separator") + "test.properties"; // 构造文件名 //System.out.println(System.getProperty("file.separator")); Properties p = new Properties(); Logger logger = Logger.getLogger("net.zukowski.ibm");
logger.log(Level.INFO, "debug"); try {
FileInputStream in = new FileInputStream(pFilename); // 构造文件的输入流
p.load(in); // 读入属性
Enumeration t = p.propertyNames();
while (t.hasMoreElements()) {
System.out.println( (String) t.nextElement());
}
in.close();
}
catch (Exception e) {
//StackTraceElement elements[] = e.getStackTrace();
StackTraceElement elements[] = e.getStackTrace();
for (int i = 0; i < elements.length; i++) {
logger.log(Level.WARNING, elements[i].getMethodName()); }
System.out.println("Error of create input stream");
} System.out.println(p.getProperty("property1"));
p.setProperty("property3", "value8"); // 给property1赋新的值 try {
FileOutputStream out = new FileOutputStream(pFilename);
p.store(out, "This file is a test"); // 设置属性文件的文件头信息
out.flush();
out.close();
}
catch (Exception e) {
System.out.println("Error of write input stream");
}
}
}