Java路径读取、开发工具(eclipse等)读取路径

来源:互联网 发布:我国旅游业的新闻数据 编辑:程序博客网 时间:2024/05/18 02:11

classpath:存放.class等编译后文件的路径

问题的探究源于eclipse等开发软件的读取路径默认为项目的根路径

part1:doc下的Java和javac -classpath用法 
javac:如果要编译的java文件引用了其它类,但是该类的.class文件不在当前目录下,这种情况下就需要在javac命令后面加上-classpath参数,通过使用以下三种类型的方法来指导编译器在编译的时候去指定的路径下查找引用类。 
1、绝对路径: 
javac -classpath c:/junit3.8.1/junit.jar Xxx.java 
2、相对路径 
javac -classpath ../junit3.8.1/junit.jar Xxx.java 
3、系统变量 
javac -classpath %CLASSPATH% Xxx.java

注意: 
1、何时需要用-classpath:当你要编译(javac)或者执行(java)的类引用了其它的类,但是被引用的.class不在当前编译文件夹或者不在%CLASSPATH%目录下时,就需要通过-classpath来引入类,例如: 
javac -classpath c:/junit3.8.1/junit.jar Xxx.java 
2、何时需要指定路径:%CLASSPATH%是用来指定.class文件的,不是用来指定.java文件的路径的。javac:当你要编译的类所在的目录和你执行的javac命令的目录不是在同一个目录时,就需要指定源文件的路径。java文件即使在%CLASSPATH%目录下也要指定路径-classpath。

part2:Java类,读取classpath路径 
获得资源绝对路径的方法 
1.this.getClass().getResource(“”) 
得到的是当前类class文件的URI目录不包括自己。 
2.this.getClass().getResource(“/”) 
得到当前的classpath的绝对URI路径 
3.this.getClass().getClassLoader().getResource(“”) 
得到的也是当前Classpath的绝对URI路径 
ClassLoader还提供了两个方法用于从装载的类路径中取得资源 
public URL getResource(string name); 
public InputStream getResourceAsStream(String name);

part3:Java通过路径读取文件 
1、在Java开发工具eclipse等中,一般是project的相对路径 
例:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">src/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">com</span>/lavasoft/res/a<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

*不以“/”开头 
*脱离了IDE环境这个写法是错误的。

2、通过CLASSPATH读取包内文件 
读取包内文件,使用路径一定是相对的classpath路径,比如a,位于包内,创建读取a的字节流

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">InputStream <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> = ReadFile<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getResourceAsStream</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/com/lavasoft/a.txt"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

注意: 
1、Class.getResourceAsStream(String path): path不以”/”开头时,默认是从此类所在的包下取资源。以“/”开头是从classpath根下获取。class类最终还是delegate授权给ClassLoader类获取资源。这方法只是通过path构造绝对路径。 
2、Class.getClassLoader.getResourceAsStream(String Path)默认从classpath下获取,不以“/”开头。 
3、ServletContext.getResourceAsStream(String Path)默认从Webapp根目录下取资源,Tomcat下是否以“/”开头无所谓。 
4、JSP的application内置对象就是上面的ServletContext的一种实现。

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">package my; import java.io.File; import java.io.IOException; import java.net.URL; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> MyUrlDemo {     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>(String[] args) {         MyUrlDemo muDemo = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MyUrlDemo();         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> {             muDemo.showURL();         } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) {             <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// TODO Auto-generated catch block</span>             e.printStackTrace();         }     }     <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">showURL</span>() throws IOException {         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes</span>         File f = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> File(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getClass().getResource(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/"</span>).getPath());         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(f);         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录  D:\git\daotie\daotie\target\classes\my</span>         File f2 = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> File(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getClass().getResource(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>).getPath());         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(f2);         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第二种:获取项目路径    D:\git\daotie\daotie</span>         File directory = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> File(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 参数为空</span>         String courseFile = directory.getCanonicalPath();         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(courseFile);         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第三种:  file:/D:/git/daotie/daotie/target/classes/</span>         URL xmlpath = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">this</span>.getClass().getClassLoader().getResource(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span>);         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(xmlpath);         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第四种: D:\git\daotie\daotie</span>         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(System.getProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"user.dir"</span>));         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*          * 结果: C:\Documents and Settings\Administrator\workspace\projectName          * 获取当前工程路径          */</span>         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 第五种:  获取所有的类路径 包括jar包的路径</span>         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(System.getProperty(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"java.class.path"</span>));     } }</code>
0 0
原创粉丝点击