Resource path in Java resource path

Source: Internet
Author: User

Java needs to know where the file is when it is operating the file. Each file is a resource, and Java obtains resources in two ways:

1. Through classes in the Java.io package, such as file, other classes and file similar

2. Through class or ClassLoader

First look at the file class:

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 the following components:

    1. An optional system-dependent prefix strings, 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.
The official document says that the file class is an abstract representation of files and directory names, does not depend on the system, and is aware of this sentence, which is reflected in the following test code.

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");//Pay attention to the execution of the sentence, even if the file does not exist, the file will not be created, but the file class object is constructed, this document does not produce//   File.createnewfile ();//file cannot recursively create files and folders System.out.println (File.getabsolutepath ()); File File2=new file ("/dir/1.txt");//absolute path System.out.println (File2.getabsolutepath ()); File File3=new file ("1.txt"), try {file3.createnewfile ();} catch (IOException e) {//TODO auto-generated catch Blocke.prin Tstacktrace ();} Feasible System.out.println (File3.getabsolutepath ()); File File4=new file ("2.txt");//Relative Road 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");//Absolute Road Strength 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 ();}}




Here is the result of the printout:

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

Only 0.gif of these files is present before execution, only one E:\Eclipse\javaStudyProjects\Reflect\1.txt is executed after execution, and even if the file does not exist, it is called New file. This file will not be created, only the object of the file class has been constructed, and this document will not be generated, confirming an abstract representation of file and directory pathnames. The file class is just an abstract representation, not a true document, and you need to call the CreateNewFile () method to create the file. If the file already exists, the CreateNewFile () method returns false and is not created again.


About the pathname:

Add the "/" prefix to the absolute path, see FILE2 and File7

A relative path is not prefixed, and this relative is relative to the path of E:\Eclipse\javaStudyProjects\Reflect\, which is User.dir



and look at class.

Printing results:

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

It can be seen that the prefix "/" does not work, because/is filtered out, the final result is equivalent to the relative path, and this relative path is e:/eclipse/javastudyprojects/reflect/bin/, That is, Java.class.path, which is the path of the top-level package of the package that is currently running the class; see Class.getResource () Source code

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;



Looking at ClassLoader:

Printing results:

File:/e:/eclipse/javastudyprojects/reflect/bin/rc/0.gif
Null
Null
Null
file:/e:/eclipse/javastudyprojects/reflect/bin/
E:\Eclipse\javaStudyProjects\Reflect\bin

It can be seen that ClassLoader does not handle the prefix "/", so plus/is wrong, return null, others are the same as class, because class still calls the ClassLoader GetResource () method




Resource path in Java resource path

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.