class Student{
private String name;
private String path;
public Student(Class s){
String tempPath = s.getProtectionDomain().getCodeSource().getLocation().getFile();//---①
try{
tempPath = java.net.URLDecoder.decode(tempPath,"UTF-8");//---②
}catch(Exception e){
e.printStackTrace();
}
java.io.File jarFile = new java.io.File(tempPath);//---③
this.name=jarFile.getName();
java.io.File parent = jarFile.getParentFile();//---④
if(parent!=null){
this.path=parent.getAbsolutePath();//---⑤
}
}}
private String name;
private String path;
public Student(Class s){
String tempPath = s.getProtectionDomain().getCodeSource().getLocation().getFile();//---①
try{
tempPath = java.net.URLDecoder.decode(tempPath,"UTF-8");//---②
}catch(Exception e){
e.printStackTrace();
}
java.io.File jarFile = new java.io.File(tempPath);//---③
this.name=jarFile.getName();
java.io.File parent = jarFile.getParentFile();//---④
if(parent!=null){
this.path=parent.getAbsolutePath();//---⑤
}
}}
解决方案 »
- 我从网上下了一个Web项目,怎么没见数据库文件在哪呀
- jsp目录的问题,请教
- 求一份java开发的聊天软件的毕业设计,谢谢
- STRUTS中如何提交一个table中的多行数据?
- 求大神帮忙啊。。毕设中的问题。。急求。。
- 高手帮我
- 高人指路!Java编程实现问题
- 我怎么在自己建立的XML文档加命名空间.?请教
- 正在看《Mastering EJB(II)》想和看过以及正在看的人一起讨论分析
- AES加密与解密报错:javax.crypto.BadPaddingException: Given final block not properly pad
- j2ee框架做通讯接入,靠不靠谱?
- 怎么使用HTML原始标签替代struts自带标签???
获取所在文件夹的“绝对路径”
第二句:使用UTF-8的编码格式输出路径;
第三句:用路径作参数建立文件;
第四句:获得父文件,及上一级文件
第五句:获取绝对路径
/E:/workspace/myeclipse/workspace/InfoPla/WebRoot/WEB-INF/classes/ //2 用utf-8编码转换
classes //3 拿到当前class所在文件夹名
java.io.File parent = jarFile.getParentFile();//---④ 拿到上级目录文件
E:\workspace\myeclipse\workspace\InfoPla\WebRoot\WEB-INF //4 拿到上级目录地址。其实试下就应该出来了
② 用于还原URL格式编码的中文和特殊字符(如空格)。
③ 取classes目录或jar文件
④ 取classes目录的上级目录或jar文件所在的目录
⑤ 取上述目录的全路径字符串
② 用于还原URL格式编码的中文和特殊字符(如空格)。
③ 取classes目录或jar文件
④ 取classes目录的上级目录或jar文件所在的目录
⑤ 取上述目录的全路径字符串