One, foreword

The path in Java has always been ambiguous feeling, for this piece, seem to know, actually do not know. Every time I see a FileNotFound, I go to Google to find it. How do I use this method? In fact, is in the answer, according to the programming is hit the lucky type of programming, this time for a one-time understanding of the Java path problem, for the future programming heart has a gully.

Relative path and absolute path

Absolute Path: The full name of an Absolute Path is a real Path. It is the complete Path in the computer. It must be accurate, or it cannot be found

“Relative path:” the so-called relative path, is relative to their target file location. Relative paths are more important than relative paths. Is it the root directory or the classpath directory of a project that is relative to it

The absolute and relative paths of the A.txt file in the resoure directory of the javaIO module are as follows:

“Absolute path:” D:\code\River\JavaSE\javaIO\ SRC \main\resources\a.txt

“Relative path:” Relative to the root path of the project: classpath(aka “target/classes”)

The path in the File class

In project, the root of the relative path is the root folder of project.

“Relative path:” D:\code\River\JavaSE (i.e. the relative path of the project), note that the path cannot start with a “/”

private static void filePath(a) {
        File file = new File("JavaIO/SRC/main/Java/org/huanghe/stream/Java. The difference between a character in the stream in bytes is md." ");
        System.out.println(file.exists());
        System.out.println(System.getProperty("user.dir"));
    }
Copy the code

Absolute path:

private static void filePath(a) {
        File file = new File("D:\code\River\JavaSE\javaIO\ SRC \main\ Java \org\huanghe\stream\ difference between character stream and byte stream in Java. Md");
        System.out.println(file.exists());
        System.out.println(System.getProperty("user.dir"));
    }
Copy the code

Class. GetResource and class.getLoader

In our Java development often need to get the content of the File, generally we will use the form of File to get, such as: I want to get “C :\ file.txt” at this time we need to create a File object File File =new File (” C :\ File TXT “);

Then create a file file input stream using the file class: FileInputStream in =new FileInputStream(file), and then we use that input stream to do whatever we want, but the downside of this is that our file path is hard-coded, and we don’t recommend it in Java coding

Of course, in web projects, we can get the absolute path of the file by using the getRealPath(” filename “) of the Application object, but this is not recommended. So we can use the class.getResource(path) and class.getClassLoader(path) methods to get file resources, so let’s explore the difference between the two:

4.1 class. GetResource (path)

The path parameter has two forms. One starts with a slash or the other does not start with a slash.

  • “Representations beginning with a ‘/'” : Get files from the root of the project in the classPath directory
  • ** Does not start with a “/” : ** takes the location of the class object as the root path to find
 / * ** Test class.getResource to get the file path* /
    public static void testClassGetResource(a) {
        System.out.println(_01_JavaFilePath.class.getResource(""));
 System.out.println(_01_JavaFilePath.class.getResource("/"));  } Copy the code

The output is:

File: / D: / Code/JavaSE/IO/target/classes/com/huanghe/chapter / : returns the testClassGetResource. Class this class's directoryFile: / D: / Code/JavaSE/IO/target/classes / : returns the classpath directory path of the projectCopy the code

Suppose the current project structure is as follows, how to obtain it for the following three cases:

There are three ways to get the files shown above:

 / * ** Test class.getResource to get the file path* /
    public static void testClassGetResource2(a) {
        System.out.println(_01_JavaFilePath.class.getResource("file/test.properties"));
 System.out.println(_01_JavaFilePath.class.getResource(".. /txt/01-java-filepath.txt"));  System.out.println(_01_JavaFilePath.class.getResource("/01-java-filepath.txt"));  }  Copy the code

The output is:

file:/D:/Code/JavaSE/io/target/classes/com/huanghe/chapter/one/file/test.properties

file:/D:/Code/JavaSE/io/target/classes/com/huanghe/chapter/txt/01-java-filepath.txt

file:/D:/Code/JavaSE/io/target/classes//01-java-filepath.txt
Copy the code

📢 : class getResource () and class. The getResourceAsStream () method used in path is the same.

4.2 Class. GetClassLoader (). GetResource (String path)

The parameter path in this method cannot start with a slash (/). Path indicates that the resource is fetched from the classpath

package testpackage;
public class TestMain {
    public static void main(String[] args) {
        TestMain t = new TestMain();
        System.out.println(t.getClass());
 System.out.println(t.getClass().getClassLoader());  System.out.println(t.getClass().getClassLoader().getResource(""));  System.out.println(t.getClass().getClassLoader().getResource("/"));//null  } } Copy the code

Output result:

1. class testpackage.TestMain
2. sun.misc.Launcher$AppClassLoader@ 1fb8ee3
3. file: /D: /Code/JavaSE/io/target/classes
4. null
Copy the code

Five, the summary

  1. The use of engineering relative paths is unreliable.
  2. Using the CLASSPATH path is reliable.
  3. As far as possible, place the files your application reads in the CLASSPATH so that they can be read during development and release.

This article is formatted using MDNICE