当前位置: 移动技术网 > IT编程>开发语言>Java > java获取各种路径的基本方法

java获取各种路径的基本方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

红尘仙劫,艾诺novo7普及版,李观来

本文实例为大家分享了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"));
 }

}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网