要得到web application中的classes路径可以使用如下方法:
1.先查出是WINDOWS操作系统还是LINUX操作系统:

private boolean isWindows()
{
String operatorSystemName = System.getProperty("os.name");
operatorSystemName = operatorSystemName.toUpperCase();

if(operatorSystemName.indexOf("WIN")>0)
{
return true;
}
return false;
}
2.得到classes路径:
String path = null;
path = this.getClass().getResource("/") + "Senders.txt";
path = path.substring(6,path.length());

if(!this.isWindows())
{
path += "/" + path;
}
例外,在windows一般文本换行符为:"\r\n",而在liunx之中则为:"\n".
1.先查出是WINDOWS操作系统还是LINUX操作系统:

private boolean isWindows()
{
String operatorSystemName = System.getProperty("os.name");
operatorSystemName = operatorSystemName.toUpperCase();
if(operatorSystemName.indexOf("WIN")>0)
{
return true;
}
return false;
}
String path = null;
path = this.getClass().getResource("/") + "Senders.txt";
path = path.substring(6,path.length());
if(!this.isWindows())
{
path += "/" + path;
}
本文介绍了一种在Java中获取Web应用程序中classes路径的方法,并针对不同的操作系统(Windows和Linux)进行了适配。此外,还提供了关于不同操作系统下文本换行符的注意事项。
1万+

被折叠的 条评论
为什么被折叠?



