zoukankan      html  css  js  c++  java
  • 浅谈getResource方法

    项目经常会读取一些配置文件, 因此getResource方法便能够起到重要作用

    使用时主要是两种方法, 一个是字节码文件Class类, 另一个是ClassLoader类加载器

    使用Class类时有两种使用方式:

    1. 使用"/"  获取到的是classpath路径

    2. 不使用"/" 这就是相对路径

    ClassLoader类

    没有"/"的写法, 获取到的就是classpath路径

    测试代码

    public class GetResourceTest {
    
        public static void main(String[] args) {
            System.out.println(GetResourceTest.class.getResource("/test.properties").getPath());
            System.out.println(GetResourceTest.class.getResource("test1.properties").getPath());
            System.out.println(GetResourceTest.class.getClassLoader().getResource("test.properties").getPath());
            System.out.println(GetResourceTest.class.getClassLoader().getResource("/"));
        }
    
    }

    结果:

    /E:/IdeaJava/studyTest/out/production/studyTest/test.properties
    /E:/IdeaJava/studyTest/out/production/studyTest/com/waston/Test/test1.properties
    /E:/IdeaJava/studyTest/out/production/studyTest/test.properties
    null

    工程包结构

  • 相关阅读:
    C#生成唯一码方法
    解剖常用软件程序都用什么语言开发
    Unity3D笔记七 GUILayout
    函数的递归
    函数
    函数的参数
    函数的返回值
    函数的定义
    文件处理
    集合
  • 原文地址:https://www.cnblogs.com/wt20/p/8320346.html
Copyright © 2011-2022 走看看