java获得当前文件路径
2015-09-10 11:26
323 查看
第一种:
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
获取当前类的所在工程路径;
如果不加“/”
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;
第二种:
File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;
第三种:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
结果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径
第四种:
System.out.println(System.getProperty("user.dir"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径
第五种:
System.out.println( System.getProperty("java.class.path"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
获取当前工程路径
通过代码获取jar中的资源,这里就不能path了,要用InputStream:
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin
获取当前类的所在工程路径;
如果不加“/”
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);
结果:
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;
第二种:
File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;
第三种:
URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);
结果:
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径
第四种:
System.out.println(System.getProperty("user.dir"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径
第五种:
System.out.println( System.getProperty("java.class.path"));
结果:
C:\Documents and Settings\Administrator\workspace\projectName\bin
获取当前工程路径
通过代码获取jar中的资源,这里就不能path了,要用InputStream:
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
相关文章推荐
- Java JVM(九):JDK8的集合流式操作
- 生产者-消费者java
- JSP中java脚本显示所有的书籍列表
- Java与时间相关的类
- java字符串反转方法总结
- MyEclipse注册码,有效期至2017年
- spring service方法里有两个save时的同步问题
- Java正则表达式Pattern和Matcher详解
- java常见内存溢出(OOM)解决方案
- eclipse安装spring插件
- MyEclipse8.5中属性设置集锦(不定时更新)
- ubuntu java环境变量配置
- Java数据类型
- eclipse res里面的资源文件命名问题
- 使用Eclipse的wsdl2java工具
- myeclipse经常弹出Subversion Native Library Not Available框解决办法
- Java中对浮点数精度的控制
- 重拾java系列一java基础(1)
- Eclipse启动时卡死解决方法
- Thinking in java 之:容器介绍