用 eclipse和tomcat开发,要修改WEB-INF/classes下的一个配置文件,在开发环境(eclipse和tomcat)测试的时候能够读取、修改,但是把该项目发布到was6上的时候,就报‘系统找不到指定的路径,; 程序是这样写的:
public static void writeKeyData(String key, String value) {
String PROPERTY_FILE = "/com/ts/config/ts.properties";
Properties prop = new Properties();
try { File file = new File(loadProp.getClass().getResource("/").getPath());
file = new File(file.getPath() + PROPERTY_FILE); if (!file.exists())
file.createNewFile();
InputStream fis = new FileInputStream(file);
prop.load(fis);
fis.close(); prop.setProperty(key, value);
OutputStream fos = new FileOutputStream(file.getPath());
prop.store(fos, null);
fos.close();
} catch (IOException e) {
e.printStackTrace();;
System.err.println("Visit " + PROPERTY_FILE + " for updating "
+ value + " value error");
}
}
was6是怎样取得WEB-INF/classes的路径的呢???? 望各位指教
public static void writeKeyData(String key, String value) {
String PROPERTY_FILE = "/com/ts/config/ts.properties";
Properties prop = new Properties();
try { File file = new File(loadProp.getClass().getResource("/").getPath());
file = new File(file.getPath() + PROPERTY_FILE); if (!file.exists())
file.createNewFile();
InputStream fis = new FileInputStream(file);
prop.load(fis);
fis.close(); prop.setProperty(key, value);
OutputStream fos = new FileOutputStream(file.getPath());
prop.store(fos, null);
fos.close();
} catch (IOException e) {
e.printStackTrace();;
System.err.println("Visit " + PROPERTY_FILE + " for updating "
+ value + " value error");
}
}
was6是怎样取得WEB-INF/classes的路径的呢???? 望各位指教
最终找到的方法是: ClassPathResource resource = new ClassPathResource(PROPERTY_FILE);
String path=null;
try {
path=resource.getURL().getPath().replace("20%", "");
} catch (Exception e) {
System.out.println(e.toString());
}
这样读取路径就不报错了