Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the absolute path and relative path in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you how to get the absolute path and relative path in Java, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Foreword:

In pure Java code, we usually use class.getResource (String name) or class.getClassLoader (). GetResource (String name) to get the address of the file (of course, more than these two methods). Today, let's talk about the similarities and differences between the two methods. The pure Java code here is not a Java web project.

1. Directory structure

2.class.getResource (String name)

Input: can accept relative path (relative to the class class) or absolute path (the root directory symbol is / represents the root directory of the project does not represent the root directory of the hard disk)

Return: URL object that represents a resource pointing to name

/ / the relative path is obtained from the sibling directory System.out.println of the class file ("relative path: configuration file in the sibling directory >" + Main.class.getResource ("demo.properties")); / / the absolute path obtained is relative to the System.out.println of the directory ("absolute path: configuration file in the sibling directory >" + Main.class.getResource ("/ com/xing/demo/demo.properties")) System.out.println ("relative path: configuration file in the previous directory >" + Main.class.getResource (".. / xing.properties"); System.out.println ("absolute path: configuration file in the root directory >" + Main.class.getResource ("/ src.properties")); / / when the absolute path is passed in (with'/'), the getresource () method parses the path address from the root directory of the project

Output:

Relative path: configuration file in the same directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/demo/demo.properties

Absolute path: configuration files in the same directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/demo/demo.properties

Relative path: configuration file in the previous directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/xing.properties

Absolute path: configuration file in the root directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/src.properties

3.class.getClassLoader () getResource (String name)

Input: only relative paths can be accepted, but this relative path is relative to the root directory

Return: URL object

System.out.println ("relative path: configuration file one level below the relative root directory >" + Main.class.getClassLoader () .getResource ("com/xing/xing.properties"))

Output:

Relative path: configuration file in the same directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/src.properties

Relative path: configuration file in the next level of the relative root directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/com.properties

Relative path: configuration file in the directory below the relative root directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/xing.properties

Note:

/ * the following two have the same effect * / System.out.println ("= = relative path: configuration file in the same level directory >" + Main.class.getResource ("demo.properties"); System.out.println ("= = relative path: configuration file in the directory below the relative root directory >" + Main.class.getClassLoader () .getResource ("com/xing/demo/demo.properties"))

Output:

= = relative path: configuration file in the same directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/demo/demo.properties

= = relative path: configuration file below the relative root directory > file:/E:/ideawork/pathdemo/out/production/pathdemo/com/xing/demo/demo.properties

3.1 differences

The difference between the two:

In fact, we can see from the source code that class.getResource actually uses class.getClassLoader (). GetResource (String name).

It's just that class.getResource uses the resolveName method to convert all incoming paths to paths that match class.getClassLoader (). GetResource () and let getClassLoader handle it.

3.2ClassLoader

Class.getClassLoader (). GetResource (String name) uses ClassLoader, and there are many ways to get ClassLoader.

How to get classLoader:

Thread.currentThread () .getContextClassLoader ()

ClassLoader.getSystemClassLoader ().

Class.getClassLoader ()

System.out.println (Thread.currentThread (). GetContextClassLoader (). GetResource ("src.properties")); System.out.println (ClassLoader.getSystemClassLoader (). GetResource ("src.properties"))

Output:

File:/E:/ideawork/pathdemo/out/production/pathdemo/src.properties

File:/E:/ideawork/pathdemo/out/production/pathdemo/src.properties

Expand: get the hard disk directory of the project to the directory at the project name level

/ / output: e:\ sparkwork\ pathdemoSystem.out.println (System.getProperty ("user.dir")); 3.3 some knowledge about URL URL fileURL = Main.class.getResource ("/ src.properties"); / / output: file:/E:/sparkwork/pathdemo/out/production/pathdemo/src.propertiesSystem.out.println (fileURL.toURI ()); / / output: / E:/sparkwork/pathdemo/out/production/pathdemo/src.propertiesSystem.out.println (fileURL.getPath ()) / / both of these methods are possible. File file = new File (fileURL.toURI ()); Filw file2 = new File (fileURL.getPath ()) is all the content of this article entitled "how to get absolute and relative paths in Java". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report