不可能是.ini等这样的格式,而是properties.
1。 使用java.util.Properties类的load()方法示例:InputStreamin=lnewBufferedInputStream (newFileInputStream(name));Propertiesp=newProperties();p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法示例:ResourceBundlerb=ResourceBundle.getBundle(name,Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数示例:InputStreamin= newBufferedInputStream(newFileInputStream(name));ResourceBundlerb=newPropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法示例:InputStreamin= JProperties.class.getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream ()方法示例:InputStreamin=JProperties.class.getClassLoader(). getResourceAsStream(name);Propertiesp=newProperties();p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法示例: InputStreamin=ClassLoader.getSystemResourceAsStream(name);Propertiesp=newProperties();p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例: InputStreamin=context.getResourceAsStream(path);Propertiesp=newProperties();p.load(in);
代码实例:
---------------------------------------------------------------------------------------------
package com.xing.db;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class LoadConn {
String name="";
String password="";
String driver ="";
String str ="";
java.sql.Connection conn;
private java.sql.Connection getConnection(){
InputStream in = this.getClass().getResourceAsStream("conn.properties");
Properties p = new Properties();
try {
p.load(in);
name=p.getProperty("name");
password = p.getProperty("password");
driver = p.getProperty("driver");
str = p.getProperty("str");
System.out.println(name+" "+driver);
try {
Class.forName(driver);
conn = DriverManager.getConnection(str, name, password);
if(conn!=null){
System.out.println("Connection successful");
}
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
LoadConn lc = new LoadConn();
lc.getConnection();
}
}
properties文件的使用
properties文件顾名思义,属性文件,从它的名称中直观的理解就是,它应该是可以表示某些属性,
是的,可以在它里面定义一些字段,这将不需要我们在代码中书写,这就可以将这些信息从代码中分
离出来了,很方便。我是在编写数据库代码的过程中接触到这个文件的,感觉确实不错!
下面我给出两个例子,直观的看一下两者的区别:
首先我先将properties文件给出,文件名为myProperties.properties
String driver = com.mysql.jdbc.Driver
String url = jdbc:mysql://localhost/myDB;
String name = root
String password = 123
例子一:
String driver = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost/myDB";
String name = "root"
String password = "123"
Class.forName(driver);
Connection con = DriverManager.getConnection(url,name,password);
例子二:
String driver;
String url;
String name;
String password;
FileInputStream fis = new FileInputStream(myProperties.properties);
Properties properties = new Properties();
properties.load(fis); //从输入流中读取属性文件的内容
fis.close();
//从属性文件中读取相应字段的信息
driver = properties.getProperty(driver);
url = properties.getProperty(url);
name = properties.getProperty(name);
password = properties.getProperty(password);
Class.forName(driver);
Connection con = DriverManager.getConnection(url,name,password);
我们看到数值和代码已经分离,这样很方便我们修改数值!
再有一定要注意properties文件中的字段的写法,不要再多添“”否则会出现问题!
因为getProperty()方法返回的是一个字符串!