1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java的相对路径和绝对路径_Java 相对路径和绝对路径的区别

java的相对路径和绝对路径_Java 相对路径和绝对路径的区别

时间:2024-07-02 16:20:39

相关推荐

java的相对路径和绝对路径_Java 相对路径和绝对路径的区别

一、概念

1、相对路径-顾名思义,相对路径就是相对于当前文件的路径。网页中一般表示路径使用这个方法。

2、绝对路径-绝对路径就是你的主页上的文件或目录在硬盘上真正的路径。绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,比如,你的Perl 程序是存放在 c:/apache/cgi-bin 下的,那么 c:/apache/cgi-bin就是cgi-bin目录的绝对路径

二、路径使用

不管你是新手还是老鸟,在程序中读取资源文件总会遇到一些找不到文件的问题,这与Java底层的实现有关,不能算bug,只要方法得当,问题还是可以解决的。

项目的文件夹结构:

1、在Java开发工具的project中使用相对路径

在project中,相对路径的根目录是project的根文件夹,在此就是repathtest文件夹了。

创建文件的写法是:

File f = new File("src/com/lavasoft/res/a.txt");

File f= new File("doc/b.txt");

注意:

路径不以“/”开头;

脱离了IDE环境,这个写法就是错误的,也并非每个IDE都如此,但我见到的都是这样的。

2、通过CLASSPATH读取包内文件

读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:

InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt");

有了字节流,就能读取到文件内容了。

注意:

这里必须以“/”开头;

3、看看完整的测试代码

packagecom.lavasoft.test;import java.io.*;/*** Java读取相对路径的文件

*

*@authorleizhimin -1-15 10:59:43*/

public classReadFile {public static voidmain(String[] args) {

readTextA_ByClassPath();

readTextA_ByProjectRelativePath();

readTextB_ByProjectRelativePath();

}/*** 通过工程相对路径读取(包内)文件,注意不以“/”开头*/

public static voidreadTextA_ByProjectRelativePath() {

System.out.println("-----------------readTextA_ByProjectRelativePath---------------------");

File f= new File("src/com/lavasoft/res/a.txt");

String a= file2String(f, "GBK");

System.out.println(a);

}/*** 通过工程相对路径读取(包外)文件,注意不以“/”开头*/

public static voidreadTextB_ByProjectRelativePath() {

System.out.println("-----------------readTextB_ByProjectRelativePath---------------------");

File f= new File("doc/b.txt");

String b= file2String(f, "GBK");

System.out.println(b);

}/*** 通过CLASSPATH读取包内文件,注意以“/”开头*/

public static voidreadTextA_ByClassPath() {

System.out.println("-----------------readTextA_ByClassPath---------------------");

InputStream in= ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt");

String a= stream2String(in, "GBK");

System.out.println(a);

}/*** 文件转换为字符串

*

*@paramf 文件

*@paramcharset 文件的字符集

*@return文件内容*/

public staticString file2String(File f, String charset) {

String result= null;try{

result= stream2String(newFileInputStream(f), charset);

}catch(FileNotFoundException e) {

e.printStackTrace();

}returnresult;

}/*** 文件转换为字符串

*

*@paramin 字节流

*@paramcharset 文件的字符集

*@return文件内容*/

public staticString stream2String(InputStream in, String charset) {

StringBuffer sb= newStringBuffer();try{

Reader r= newInputStreamReader(in, charset);int length = 0;for (char[] c = new char[1024]; (length = r.read(c)) != -1;) {

sb.append(c,0, length);

}

r.close();

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}returnsb.toString();

}

}

(代码写得粗糙,异常没做认真处理)

运行结果:

-----------------readTextA_ByClassPath---------------------testtestsetsetsetsetsetsetsetset-----------------readTextA_ByProjectRelativePath---------------------testtestsetsetsetsetsetsetsetset-----------------readTextB_ByProjectRelativePath---------------------b.txt]b.txtb.txtb.txtb.txtb.txtb.txt

这是通过eclipse开发工具运行的,结果没问题,如果换成控制台执行,那么使用了项目相对路径的读取方式会失败,原因是,此时已经脱离了项目的开发环境,-----这个问题常常困扰着一些菜鸟,代码在开发工具好好的,发布后执行就失败了!

下面我截个图:

5.1、获取CLASSPATH下文件的绝对路径

packagecom.lavasoft.test;importjava.io.File;/*** CLASSPATH文件的绝对路径获取测试

*

*@authorleizhimin -1-18 9:33:02*/

public classTest {//classpath的文件路径

private static String cp = "/com/lavasoft/cfg/syscfg.properties";private static String cptest = "/com/lavasoft/cfg/test.properties";public static voidmain(String[] args) {//当前类路径

System.out.println(Test.class.getClassLoader().getResource("").getPath());

System.out.println(Test.class.getResource("").getFile());//当前类路径

System.out.println(OtherProject.class.getClass().getResource("/").getFile());

System.out.println(OtherProject.class.getClass().getResource(cptest).getFile());//当前类的绝对路径

System.out.println(Test.class.getResource("/").getFile());//指定CLASSPATH文件的绝对路径

System.out.println(Test.class.getResource(cp).getFile());

System.out.println(Test.class.getResource(cptest).getPath());//指定CLASSPATH文件的绝对路径

File f = new File(Test.class.getResource(cp).getFile());

File ft= new File(Test.class.getResource(cptest).getFile());

System.out.println(f.getPath());

System.out.println(ft.getPath());//File ftest = new File("src/com/lavasoft/cfg/test.properties");//String a = ReadFile.file2String(ftest, "GBK");//System.out.println( a);

}

}

5.2、获取其他项目的资源文件

这里我新建了一个工程命名为:ClassPathExampeRealt,其中 ClassPathExample 依赖了 ClassPathExampeRealt

最后结果如下:

读取ClassPathExampe/C:/Users/testMachine/mavenexample/ClassPathExample/bin/

/C:/Users/testMachine/mavenexample/ClassPathExample/bin/com/lavasoft/test/

/C:/Users/testMachine/mavenexample/ClassPathExample/bin/

/C:/Users/testMachine/mavenexample/ClassPathExample/bin/com/lavasoft/cfg/syscfg.properties/C:/Users/testMachine/mavenexample/ClassPathExampeRealt/bin/com/lavasoft/cfg/test.properties

读取ClassPathExampeRealt/C:/Users/testMachine/mavenexample/ClassPathExample/bin/

/C:/Users/testMachine/mavenexample/ClassPathExampeRealt/bin/com/lavasoft/cfg/test.properties

C:\Users\testMachine\mavenexample\ClassPathExample\bin\com\lavasoft\cfg\syscfg.properties

C:\Users\testMachine\mavenexample\ClassPathExampeRealt\bin\com\lavasoft\cfg\test.properties

可以知道,通过classpath路径 可以读取被依赖的项目下的文件;

6、读取web 项目的 meta-inf文件

项目结构如下:

packagecom.myexample;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.Reader;importjava.io.UnsupportedEncodingException;.URL;public classReadFile {public static voidmain(String[] args) {

String file= "/META-INF/test.properties";

URL fileURL= ReadFile.class.getResource(file);

System.out.println(fileURL);//readTextA_ByClassPath();

if (fileURL != null) {

}

}

}

结果:

file:/C:/Users/testMachine/mavenexample/ResourceExample/build/classes/META-INF/test.properties

总结

使用工程相对路径是靠不住的。

使用CLASSPATH路径是可靠的。

对于程序要读取的文件,尽可能放到CLASSPATH下,这样就能保证在开发和发布时候均正常读取。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。