
正文
java都去配置文件代码 java 配置文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 怎么读取web jar中的某个配置文件
项目迁移的过程中发现以前的代码维护性实在是差。
我把问题简化为以下这些简单的代码:
项目M 引用了项目 A.jar,这个A在lib目录里面
在A里面放置了一个配置文件test.properties, 就放在jar的根目录下。
A.jar
|___test.properties
在M中有一段代码回去读取这个A.jar里的配置文件,简单一点就用下面这句话来调用。
Java code
public class ConfigUtil {
public static String getInstance() throws Exception{
String path = ConfigUtil.class.getResource("/").toString();
path = path.substring(0, path.length()-8);//
System.out.println(path);//这里打印的结果显示可以拿到当前类的绝对路径
InputStream f = new FileInputStream("jar:"+path+"lib!/A.jar/"+"test.properties");
return "xxx";
}
}
相关问答
Q1: java 怎么读取配置文件代码
方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。
因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
具体举例如下:
//ServletContext.getRealPath(name)读取路径
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throwsServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //读取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServlet
Q2: 请大神帮我看看这段用java写的读取配置文件properties代码写的有没有错误,能不能优化,谢谢!
你这样写成单例模式, 如果没有其他容器来管理, 我觉得还不如直接把方法写成静态的算了.
返回参数我觉得返回一个Properties就可以了, 没必要返回HashMap
in用完了最好close一下
Q3: Java中的properties配置文件怎么写,代码
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id", "user1");
p.setProperty("password", "123456");
try{
PrintStream stm = new PrintStream(new File("e:\test.properties"));
p.list(stm);
} catch (IOException e) {
e.printStackTrace();
}
}
Q4: JAVA零配置读取配置文件
package resources;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class DbUtil {
public static Connection getConnection(){
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
//读取类路径下java都去配置文件代码的 jdbc.properties 文件java都去配置文件代码,我的配置文件放在src包下
InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("username");
password = properties.getProperty("password");
Driver driver = null;
try {
driver = (Driver) Class.forName(driverClass).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
//通过 Driver 的 connect 方法获取数据库连接.
Connection connection = null;
try {
connection = driver.connect(jdbcUrl, info);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----The database is connected----");
return connection;
}
}
jdbc.properties内容如下java都去配置文件代码:
driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc\:oracle\:thin\:@10.91.4.102\:1521\:orcljdbcUrl=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=cp2
password=cp2test
//调用方法
public static void main(String[] args) {
String id = "111111";
String gread = "3";
ListStudent sList = new ArrayListStudent();
Connection con = DbUtilCR.getConnection();
PreparedStatement pre = null;
ResultSet result = null;
String sql = "select s.name,s.age from student s where s.id=? and s.gread=?";
try {
pre = con.prepareStatement(sql);
pre.setString(1, id);//传参数学号
pre.setString(2, gread);//传参数年级
result = pre.executeQuery();
System.out.println("执行SQL为:["+sql+"]");
System.out.println("参数为:["+id+","+gread+"]");
while (result.next()){
Student st = new Student();
st.setName(result.getString("name"));//与查询出的字段或者别名保持一致
st.setAge(result.getString("age"));
sList.add(st);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0;isList.size();i++) {
System.out.println("姓名:"+sList.get(i).getName()+"\t年龄:"+sList.get(i).getAge());
}
}
java都去配置文件代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 配置文件、java都去配置文件代码的信息别忘了在本站进行查找喔。







