当前位置: 技术问答>java相关
在java中生成文件时如何使用系统环境变量来指标文件路径?(在线等待)
来源: 互联网 发布时间:2015-09-11
本文导语: 例如要生成文件名为fileName FileOutputStream out=new FileOutputStream("..//webapps//ROOT//img/"+fileName); 想把“..//webapps//ROOT//img/”替换成环境变量名,如何实现? | vrmlman(vrmlman) 的方法是可以做到的! 先把...
例如要生成文件名为fileName
FileOutputStream out=new FileOutputStream("..//webapps//ROOT//img/"+fileName);
想把“..//webapps//ROOT//img/”替换成环境变量名,如何实现?
FileOutputStream out=new FileOutputStream("..//webapps//ROOT//img/"+fileName);
想把“..//webapps//ROOT//img/”替换成环境变量名,如何实现?
|
vrmlman(vrmlman) 的方法是可以做到的!
先把这些路径写在配置文件中XXX.properties文件中,如何写一个类进行实际与虚拟路径的转换。
配制文件为:
reportpath=D:/WebSphere/AppServer/installedApps/GMIMS.ear.ear/gmims.war/report/
reporturl=/report/
转换的类为:
public Config()
{
reportPath = "c:/";
reportURL = "http://localhost/report/";
FileInputStream fio = null;
Properties proInfo = new Properties();
try
{
fio = new FileInputStream("c:\XXX.properties");
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException While Open File:" + ex.toString());
}
catch(SecurityException ex)
{
System.out.println("SecurityException While Open File:" + ex.toString());
}
catch(Exception ex)
{
System.out.println("Exception While Open File:" + ex.toString());
}
try
{
proInfo.load(fio);
}
catch(IOException ex)
{
System.out.println("IOException While Load Info:" + ex.toString());
}
try
{
fio.close();
}catch(Exception ex)
{
System.out.println("IOException While Close File:" + ex.toString());
}
setReportPath(proInfo.getProperty("reportpath", "c:/"));
setReportURL(proInfo.getProperty("reporturl", "http://localhost/myapp/report/"));
}
public String getReportPath()
{
return reportPath;
}
public String getReportURL()
{
return reportURL;
}
private void setReportPath(String newReportPath)
{
reportPath = newReportPath;
}
private void setReportURL(/tech-qa-java/String newReportURL/index.html)
{
reportURL = newReportURL;
}
}
希望能给点帮助!
先把这些路径写在配置文件中XXX.properties文件中,如何写一个类进行实际与虚拟路径的转换。
配制文件为:
reportpath=D:/WebSphere/AppServer/installedApps/GMIMS.ear.ear/gmims.war/report/
reporturl=/report/
转换的类为:
public Config()
{
reportPath = "c:/";
reportURL = "http://localhost/report/";
FileInputStream fio = null;
Properties proInfo = new Properties();
try
{
fio = new FileInputStream("c:\XXX.properties");
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException While Open File:" + ex.toString());
}
catch(SecurityException ex)
{
System.out.println("SecurityException While Open File:" + ex.toString());
}
catch(Exception ex)
{
System.out.println("Exception While Open File:" + ex.toString());
}
try
{
proInfo.load(fio);
}
catch(IOException ex)
{
System.out.println("IOException While Load Info:" + ex.toString());
}
try
{
fio.close();
}catch(Exception ex)
{
System.out.println("IOException While Close File:" + ex.toString());
}
setReportPath(proInfo.getProperty("reportpath", "c:/"));
setReportURL(proInfo.getProperty("reporturl", "http://localhost/myapp/report/"));
}
public String getReportPath()
{
return reportPath;
}
public String getReportURL()
{
return reportURL;
}
private void setReportPath(String newReportPath)
{
reportPath = newReportPath;
}
private void setReportURL(/tech-qa-java/String newReportURL/index.html)
{
reportURL = newReportURL;
}
}
希望能给点帮助!
|
如果依赖于环境变量,我所用的方法是针对不同的系统(没法子),在启动程序之前把环境变量的内容写入文件。直接从环境变量里面读取就不知道如何来做了,关注。
Linux、FreeBSD 等:
在shell脚本里 cat $MY_HOME > pathfile
然后从pathfile 读取
Dos 当然不能用cat了,可以用type,呵呵。
Linux、FreeBSD 等:
在shell脚本里 cat $MY_HOME > pathfile
然后从pathfile 读取
Dos 当然不能用cat了,可以用type,呵呵。
|
我以前试过,但是没有成功, 后来用的方法跟 huang_brid(如风--风之舞---水风轻吻) 说的差不多