搜索
首页类库下载java类库java中获取路径的几种基本的方法

package com.ygh.blog.realpath;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * 获取java下面的路径的演示
 */
import org.junit.Test;

public class RealPathTest {

    /**
     * 获取当前类所在的工程路径
     */
    @Test
    public void fun1() {
        File file = new File(this.getClass().getResource("/").getPath());
        // D:\project\taotaoshop\src\blog-mybatis1\target\test-classes
        System.out.println(file);
    }

    /**
     * 获取当前类的绝对路径
     */
    @Test
    public void fun2() {
        File file = new File(this.getClass().getResource("").getPath());
        // D:\project\taotaoshop\src\blog-mybatis1\target\test-classes\com\ygh\blog\realpath
        System.out.println(file);
    }

    /**
     * 获取当前类所在的工程路径,两种方法皆可
     * 
     * @throws IOException
     */
    @Test
    public void fun3() throws IOException {
        File file = new File("");
        String path = file.getCanonicalPath();
        // D:\project\taotaoshop\src\blog-mybatis1
        System.out.println(path);
        // D:\project\taotaoshop\src\blog-mybatis1
        System.out.println(System.getProperty("user.dir"));
    }

    /**
     * 获取当前src下面的文件的路径
     */
    @Test
    public void fun4() {
        URL url = this.getClass().getClassLoader().getResource("jdbc.properties");
        System.out.println(url);
    }

    /**
     * 获取其他源码包下面的文件路径
     */
    @Test
    public void fun5() {
        // 使用这种方法可以获取路径
        URL url = this.getClass().getClassLoader().getResource("test2.txt");
        // file:/D:/project/taotaoshop/src/blog-mybatis1/target/classes/test.txt
        System.out.println(url);
    }

    @Test
    public void fun6() throws Exception {
        URL url = this.getClass().getClassLoader().getResource("test2.txt");
        System.out.println(url.getPath());
        Properties properties = new Properties();
        // 使用这种方式可以获取文件对应的输出流
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(inputStream);
        File file = new File(url.getPath());
        System.out.println(properties.get("jdbc.driverClassName"));
    }

}

下面赋上代码对应的文件路径

1.png

1.png

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器