tomcat下相对路径问题 我有一个web项目,在同一个包下有一个类里要读取对应的.properties文件,该怎么来表示其读取路径才能正确的读取出来这个文件?我试了很多种了都不行! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 都放classes下就可以了,路径就直接用该文件 // FrontEnd Plus GUI for JAD// DeCompiled : PropertyManager.classpackage tools;import java.io.*;import java.util.*;public class PropertyManager { private static PropertyManager manager = null; private static Object managerLock = new Object(); private static String propsName = "/elearning.properties"; private Properties properties; private Object propertiesLock; private String resourceURI; public PropertyManager() { properties = null; propertiesLock = new Object(); } public static String getProperty(String s) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.getProp(s); } public static void setProperty(String s, String s1) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } manager.setProp(s, s1); } public static void deleteProperty(String s) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } manager.deleteProp(s); } public static Enumeration propertyNames() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propNames(); } public static boolean propertyFileIsReadable() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileIsReadable(); } public static boolean propertyFileIsWritable() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileIsWritable(); } public static boolean propertyFileExists() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileExists(); } private PropertyManager(String s) { properties = null; propertiesLock = new Object(); resourceURI = s; } protected String getProp(String s) { if (properties == null) synchronized (propertiesLock) { if (properties == null) loadProps(); } String s1 = properties.getProperty(s); if (s1 == null) return null; else return s1.trim(); } protected void setProp(String s, String s1) { synchronized (propertiesLock) { if (properties == null) loadProps(); properties.setProperty(s, s1); saveProps(); } } protected void deleteProp(String s) { synchronized (propertiesLock) { if (properties == null) loadProps(); properties.remove(s); saveProps(); } } protected Enumeration propNames() { if (properties == null) synchronized (propertiesLock) { if (properties == null) loadProps(); } return properties.propertyNames(); } private void loadProps() { properties = new Properties(); InputStream inputstream = null; try { inputstream = getClass().getResourceAsStream(resourceURI); properties.load(inputstream); } catch (Exception exception) { exception.printStackTrace(); } finally { try { inputstream.close(); } catch (Exception exception2) { } } } private void saveProps() { String s = properties.getProperty("path").trim(); FileOutputStream fileoutputstream = null; try { fileoutputstream = new FileOutputStream(s); properties.store(fileoutputstream, "elearning.properties -- " + new Date()); } catch (Exception exception) { exception.printStackTrace(); } finally { try { fileoutputstream.close(); } catch (Exception exception2) { } } } public boolean propFileIsReadable() { try { InputStream inputstream = getClass().getResourceAsStream( resourceURI); return true; } catch (Exception exception) { return false; } } public boolean propFileExists() { /* * String s = getProp("path"); if (s == null) return false; File file = * new File(s); return file.isFile(); */ return true; } public boolean propFileIsWritable() { String s = getProp("path"); File file = new File(s); if (file.isFile()) return file.canWrite(); else return false; } public static void main(String[] args) { System.out.println(PropertyManager.getProperty("DEFAULTALIAS_2")); }} 楼上写那么多!!!String tomcat_path = System.getProperty("user.dir").replace("\\", "/") +"/../webapps/项目名称";//这个方法可以取得tomcat下你的项目的绝对路径 接着上面的File file = new File(tomcat_path+"你的文件名"); 包名:com.branch.common,DBPoolResources.properties是配置文件,放在com.branch.common包下。配置文件内容:db.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver程序中:import java.util.ResourceBundle;public static ResourceBundle apps=ResourceBundle.getBundle("com.branch.common.DBPoolResources");driver=apps.getString("db.driver"); 我的路径是这样的com/mps/uc/tradeandhint/dao/configure.properties在这个com/mps/uc/tradeandhint/dao/a.class我要在这个a.class里如何读取到那个configure.properties。 在a.class里:import java.util.ResourceBundle; public static ResourceBundle apps=ResourceBundle.getBundle("com/mps/uc/tradeandhint/dao/configure"); driver=apps.getString("db.driver");这样就可以了。configure.properties里面:db.driver=..... 如何创建一个文件选择器 高手请进!!! 如何用ant 编译多个源文件夹下的代码 為何只顯示This is my JSP page jsp遇到的问题,请教 如何一次操作页面上的相同元素? 初学者第一次提问,学习《JSP应用开发详解(第二版)》遇到的问题 jsp页面中是否可以将请求转发给另外一台机器的asp页面? 这么一个jsp页面:想实现可以像servU那样允许一个ip段能够访问该页 谢谢! 求助一个表单的问题 一个很奇怪关于编码的问题 求一份Java 动态生成 pdf文件的示例~
// DeCompiled : PropertyManager.class
package tools;
import java.io.*;
import java.util.*;public class PropertyManager { private static PropertyManager manager = null; private static Object managerLock = new Object(); private static String propsName = "/elearning.properties"; private Properties properties; private Object propertiesLock; private String resourceURI; public PropertyManager() {
properties = null;
propertiesLock = new Object();
} public static String getProperty(String s) {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
return manager.getProp(s);
} public static void setProperty(String s, String s1) {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
manager.setProp(s, s1);
} public static void deleteProperty(String s) {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
manager.deleteProp(s);
} public static Enumeration propertyNames() {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
return manager.propNames();
} public static boolean propertyFileIsReadable() {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
return manager.propFileIsReadable();
} public static boolean propertyFileIsWritable() {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
return manager.propFileIsWritable();
} public static boolean propertyFileExists() {
if (manager == null)
synchronized (managerLock) {
if (manager == null)
manager = new PropertyManager(propsName);
}
return manager.propFileExists();
} private PropertyManager(String s) {
properties = null;
propertiesLock = new Object();
resourceURI = s;
} protected String getProp(String s) {
if (properties == null)
synchronized (propertiesLock) {
if (properties == null)
loadProps();
}
String s1 = properties.getProperty(s);
if (s1 == null)
return null;
else
return s1.trim();
} protected void setProp(String s, String s1) {
synchronized (propertiesLock) {
if (properties == null)
loadProps();
properties.setProperty(s, s1);
saveProps();
}
} protected void deleteProp(String s) {
synchronized (propertiesLock) {
if (properties == null)
loadProps();
properties.remove(s);
saveProps();
}
} protected Enumeration propNames() {
if (properties == null)
synchronized (propertiesLock) {
if (properties == null)
loadProps();
}
return properties.propertyNames();
} private void loadProps() {
properties = new Properties();
InputStream inputstream = null;
try {
inputstream = getClass().getResourceAsStream(resourceURI);
properties.load(inputstream);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
inputstream.close();
} catch (Exception exception2) {
}
}
} private void saveProps() {
String s = properties.getProperty("path").trim();
FileOutputStream fileoutputstream = null;
try {
fileoutputstream = new FileOutputStream(s);
properties.store(fileoutputstream, "elearning.properties -- "
+ new Date());
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
fileoutputstream.close();
} catch (Exception exception2) {
}
}
} public boolean propFileIsReadable() {
try {
InputStream inputstream = getClass().getResourceAsStream(
resourceURI);
return true;
} catch (Exception exception) {
return false;
}
} public boolean propFileExists() {
/*
* String s = getProp("path"); if (s == null) return false; File file =
* new File(s); return file.isFile();
*/
return true;
} public boolean propFileIsWritable() {
String s = getProp("path");
File file = new File(s);
if (file.isFile())
return file.canWrite();
else
return false;
}
public static void main(String[] args) {
System.out.println(PropertyManager.getProperty("DEFAULTALIAS_2"));
}}
配置文件内容:
db.driver = com.microsoft.jdbc.sqlserver.SQLServerDriver程序中:
import java.util.ResourceBundle;public static ResourceBundle apps=ResourceBundle.getBundle("com.branch.common.DBPoolResources");
driver=apps.getString("db.driver");
在这个com/mps/uc/tradeandhint/dao/a.class
我要在这个a.class里如何读取到那个configure.properties。
import java.util.ResourceBundle;
public static ResourceBundle apps=ResourceBundle.getBundle("com/mps/uc/tradeandhint/dao/configure");
driver=apps.getString("db.driver");这样就可以了。configure.properties里面:
db.driver=.....