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

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

在JAR内的代码中读取resources下的文件时,需要使用getResourceAsStream,示例如下:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("group.graphqls");
 TypeDefinitionRegistry typeRegistry = schemaParser.parse(new InputStreamReader(is));

 

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
    }

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值