您的位置:首页 > 编程语言 > Java开发

Java中如何访问资源文件的路径问题

2017-05-15 17:25 471 查看
在开发项目时,经常需要使用到配置文件,则需要在代码中对该配置文件进行访问读取。本文对几种通用的访问文件的方式进行记录。

1.使用绝对路径

package com.zte.sunquan.demo.path;
public class FileCaptureTest {
@Test
public void testFileAccess1() {
String filePath = "E:\\ttp-table-map.xml";
String filePath2 = "E:/ttp-table-map.xml";
String filePath3 = "E:" + File.separator + "ttp-table-map.xml";
File f = new File(filePath);
Assert.assertTrue(f.exists());
f = new File(filePath2);
Assert.assertTrue(f.exists());
f = new File(filePath3);
Assert.assertTrue(f.exists());
}
}


如上面代码,使用绝对路径,自然没有问题。

2.Maven项目中路径

@Test
public void testFileAccess2() throws IOException {
InputStream is = FileCaptureTest.class.getResourceAsStream("/test.yang");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}


该方法使用classLoader直接获取InputStream

FileCaptureTest类存在于maven项目中的test模块中,所以其类的根路径为target/test-classes

/ 所对应的根目录即在此

由于 ./是当前目录 ../ 是父目录

@Test
public void testFileAccess3() throws IOException {
InputStream is = FileCaptureTest.class.getResourceAsStream("../../../../../../test.yang");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}


测试类存在于com.zte.sunquan.demo.path

6次../则是test-classes,所以效果是一样的

3.获取根路径后,根据代码中已知的相对路径,自己构造文件路径

@Test
public void testFileAccess4() throws IOException {
URL resource = FileCaptureTest.class.getResource("/test.yang");
//file:/E:/sunquan-project/oscp-yang-utils/yang-test/target/test-classes/test.yang
System.out.println(resource);
String path = this.getClass().getResource("/").getPath();
///E:/sunquan-project/oscp-yang-utils/yang-test/target/test-classes/
System.out.println(path);
File f = new File(path);
Assert.assertTrue(f.isDirectory());
Assert.assertTrue(f.list().length == 2);
Assert.assertTrue(f.list()[1].equals("test.yang"));
f = new File(path + File.separator + "test.yang");
Assert.assertTrue(f.isFile());
}




4.获取项目根目录

// 第二种:获取项目路径    E:\sunquan-project\demo
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);

// 第四种: E:\sunquan-project\demo
System.out.println(System.getProperty("user.dir"));


5.获取类加载的根路径或目录

// 第一种:获取类加载的根路径   E:\sunquan-project\demo\target\test-classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);
// 获取当前类的所在工程路径; 如果不加“/”  获取当前类的加载目录
// E:\sunquan-project\demo\target\classes\com\zte\sunquan\demo\path
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);


6.file path的区别:(/根 ./当前 ../父)

@Test
public void filePath() throws IOException {
File file = new File(".\\test.txt");
System.out.println(file.getPath());//.\test.txt
System.out.println(file.getAbsolutePath());//E:\sunquan-project\demo\.\test.txt
System.out.println(file.getCanonicalPath());//E:\sunquan-project\demo\test.txt
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path