java中的资源路径resource path

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:41   1786   0

java在操作文件的时候必然需要知道这个文件在哪里。每种文件就是一种资源,java获取资源的方式有两种:

1.通过java.io包中的类,例如File,其他类和File类似

2.通过Class或者ClassLoader

先看看File类:

An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:

  1. An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\\\" for a Microsoft Windows UNC pathname, and
  2. A sequence of zero or more string names.
官方文档说File类是文件和目录名的一种抽象代表,不依赖于系统,注意理解这句话,在下面的测试代码中会有所体现

import java.io.File;
import java.io.IOException;


public class TestResourcePath {

 public static void main(String[] args) throws IOException {
  TestResourcePath t = new TestResourcePath();
  t.testFile();
  t.testClass();
  t.testClassLoader();
 }
 
 public void testFile(){
  File file=new File("dir/1.txt");//注意执行该句,即使这个文件不存在,这个文件也不会被创建,只是File类的对象被构造了,这个文件并不会产生
  //file.createNewFile();//File无法递归的创建文件及文件夹
  System.out.println(file.getAbsolutePath());

     
  File file2=new File("/dir/1.txt");//绝对路径
  System.out.println(file2.getAbsolutePath());

  File file3=new File("1.txt");
  try {
   file3.createNewFile();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }//可行
  System.out.println(file3.getAbsolutePath()); 
  
  File file4=new File("2.txt");//相对路劲
  System.out.println(file4.getAbsolutePath());
  
  File file5=new File("");
  System.out.println(file5.getAbsolutePath());
  
  File file6=new File("RC/0.gif");
  System.out.println(file6.getAbsolutePath());
  
  File file7=new File("/RC/0.gif");//绝对路劲
  System.out.println(file7.getAbsolutePath());
  
  System.out.println(System.getProperty("user.dir"));
  System.out.println();
 }
 
 public void testClass(){
  System.out.println(this.getClass().getResource("RC/0.gif"));
  System.out.println(this.getClass().getResource("/RC/0.gif"));
  System.out.println(this.getClass().getResource("1.txt"));
  System.out.println(this.getClass().getResource("/1.txt"));
  System.out.println(this.getClass().getResource(""));
  System.out.println(System.getProperty("java.class.path"));
  System.out.println();
 }
 
 public void testClassLoader(){
  System.out.println(this.getClass().getClassLoader().getResource("RC/0.gif"));
  System.out.println(this.getClass().getClassLoader().getResource("/RC/0.gif"));
  System.out.println(this.getClass().getClassLoader().getResource("1.txt"));
  System.out.println(this.getClass().getClassLoader().getResource("/1.txt"));
  System.out.println(this.getClass().getResource(""));
  System.out.println(System.getProperty("java.class.path"));
  System.out.println();
 }
}




下面是打印结果:

E:\Eclipse\javaStudyProjects\Reflect\dir\1.txt
E:\dir\1.txt
E:\Eclipse\javaStudyProjects\Reflect\1.txt
E:\Eclipse\javaStudyProjects\Reflect\2.txt
E:\Eclipse\javaStudyProjects\Reflect
E:\Eclipse\javaStudyProjects\Reflect\RC\0.gif
E:\RC\0.gif
E:\Eclipse\javaStudyProjects\Reflect

这几个文件程序执行之前只有0.gif是存在的,执行之后只多了一个E:\Eclipse\javaStudyProjects\Reflect\1.txt;也即是说new File 时,即使这个文件不存在,这个文件也不会被创建,只是File类的对象被构造了,这个文件并不会产生,印证了An abstract representation of file and directory pathnames.File类只是一个抽象代表,并不是正真的文件,要想这个文件创建,需要调用createNewFile()方法。如果文件已存在,createNewFile()方法返回假,不会再次创建。


关于pathname的写法:

加“/”前缀的表示的是绝对路径,看file2和file7

不加前缀的是相对路径,并且这个相对是相对于E:\Eclipse\javaStudyProjects\Reflect\这个路径,即user.dir



再来看看Class

打印结果:

file:/E:/Eclipse/javaStudyProjects/Reflect/bin/RC/0.gif
file:/E:/Eclipse/javaStudyProjects/Reflect/bin/RC/0.gif
null
null
file:/E:/Eclipse/javaStudyProjects/Reflect/bin/

E:\Eclipse\javaStudyProjects\Reflect\bin

可以看出这个里面的前缀“/”不在起作用,因为/被过滤掉了,最后的结果都相当于相对路径,并且这个相对路径是E:/Eclipse/javaStudyProjects/Reflect/bin/,即java.class.path;也即是当前运行的类所在包的顶层包所在路径;看看Class.getResource()源码

public java.net.URL getResource(String name) {
  name = resolveName(name);
  ClassLoader cl = getClassLoader();
  if (cl==null) {
    return ClassLoader.getSystemResource(name);  // A system class.
  }
  return cl.getResource(name);
}

private String resolveName(String name) {
  if (name == null) {
    return name;
  }
  if (!name.startsWith("/")) {
    Class c = this;
    while (c.isArray()) {
      c = c.getComponentType();
    }
    String baseName = c.getName();
    int index = baseName.lastIndexOf('.');
    if (index != -1) {
      name = baseName.substring(0, index).replace('.', '/') + "/" + name;
    }
  } else {
    name = name.substring(1);
  }
  return name;
}



在看ClassLoader:

打印结果:

file:/E:/Eclipse/javaStudyProjects/Reflect/bin/RC/0.gif
null
null
null
file:/E:/Eclipse/javaStudyProjects/Reflect/bin/
E:\Eclipse\javaStudyProjects\Reflect\bin

可以看出ClassLoader不处理前缀“/”,所以加/是错误的,返回null,其他都和Class一样,因为Class还是调用了ClassLoader的getResource() 方法




分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP