Home 类库下载 java类库 Several basic methods to obtain paths in java

Several basic methods to obtain paths in java

Oct 11, 2016 pm 04:41 PM

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"));
    }

}
Copy after login

Assign the file path corresponding to the code below

Several basic methods to obtain paths in java

Several basic methods to obtain paths in java

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)