tags: javaSE Profile read Class path
The problem of always entangling the path when reading the configuration file, in fact, all path problems always escape, do not take off several keywords
Relative path, absolute path, classpath (ClassPath)
First introduce these paths first
relative path:
Relative path: As the name suggests, the path is relative, such as the current path is TEST,
If you want to locate the file under Test, you can use a relative path.
For example, there are AAAs under Test,
Then the path relative to TEST is Test / AAA.
Absolute path:
The absolute path is actually a "relative path", but it is relative to the disk (C drive, D disk waiting) or the root directory in Linux.
For example: c: \ users \ 79894 \ desktop \ csdn note
Class path:
This class path is the names of the class file, why is you using a class path in Java?
It is definitely for convenience, how can it be convenient?
For example, I wrote a project, there is a lot of source files in it, and there will be a lot of Class files after the source file is compiled.
You can't let these Java files and Class files together, that's more chaotic.
So the general IDE creates a folder specifically store class file when building a project, also called the output path Output, which stores the same directory store file with the source file directory results.
That problem is coming, talk so much, is there a hair relationship with a class path?
Then let's imagine, if there is no class path, now there is a relative path and absolute path
If you need to locate a file, the absolute path is definitely not, because the project is definitely to be transplanted.
That can only use a relative path, if you use a relative path, the best definitely is better than the root directory of the project, because you can find all the files.
So every time you have to read the file, you must write a relative path of the project, is it troublesome?
So introducing a class path - ClassPath, this path is actually the relative path of the project, but it is written to a file. ClassPath
Then when a Java program is running, just use the relative path, you will find the relative path from this .classpath file.
This method is generally used to obtain information about the configuration file.
Two ways:
1.TestClass.class.getResourceAsStream(String path);
2.TestClass.class.getClassLoader.getResourceAsStream(String path);
If PATH does not bring "/", then find the file from the path of the current class file.
If the PATH belt "/", then it is to find a file from the class path .classpath
Can you bring / because this means is to find a file in class path Classpath.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Directory Structure:
[Outer chain picture "failed, the source station may have an anti-theft chain mechanism, it is recommended to save the picture directly upload (IMG-Zisjvsbj-1626164554225) (D: \ blogpic \ image-20210713135652524.png)]
In the Maven project, the configuration file is usually placed in Resources, which is generally copied to Classes and Test-Classes in the Target path.
In classpath, there is a configuration that points to Target / Classes and Target / Test-Classes, then getResourceSstream can find the corresponding file based on this XML file.
package com.tl.test;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class TestClassPath {
public static void main(String[] args) throws Exception {
// Get file input stream
// InputStream is = TestClassPath.class.getClassLoader().getResourceAsStream("test/conf.properties");
InputStream is = TestClassPath.class.getResourceAsStream("/conf2.properties");
// Get a conversion stream resolution character encoding problem
InputStreamReader isr = new InputStreamReader(is);
/ / Get the Properties object
Properties properties = new Properties();
/ / Load resource file to Properties
properties.load(isr);
// Close the stream (you can turn it off after loading complete)
isr.close();
/ / Get the properties in the configuration file
String username = properties.getProperty("username");
System.out.println(username);
}
}
Everyone can use this class to test the use of getResourceceasstream
Foreword When learning Spring's dependency injection, it was diverted by Google to the Java Generics FAQs. This article provides an in-depth explanation of all aspects of generics in Java. After readi...
In March, jump no? >>> Explanation Before discussing the Java I / O must first discuss the following: Buffer Operations Kernel space user space Virtual Memory File I / O, stream I / O UNIX I ...
Collections in Java (on) 1. What is a collection Collection class is the realization of Java data structure. Java collection classes are an important part of the java.util package. It allows elements ...
Java uses getResource and getResourceAsStream to get the path of the file is based on the following project: the src directory is set to source root and the resources directory is set to resources roo...
Prior to want to get a resource file to do some processing, use getClass (). GetResourceAsStream () has been unable to get the file. Specific usage. Specific file and location of the code is in src...
Although there are numerous differences between the getResource () and getResourceAsStream () methods of ClassLoader and Class on the Internet, but I also encountered this problem, so I recorded it. &...
I have seen this method in the video tutorial of JAVA connection to the database. I found it on the Internet if I thought it should be important. Write it down now that you have learned it, and forget...
GetresourceASSTREAM in Java Return to NULL problem processing: The main reason for this problem is the setting of the source: In our commonly used IDEA, the input stream returned with GetResourceASS...
Reference: Spring Development Guide (Author: Xia Xin) ------------------------------------------------------------------------------------------------------------------------------- Referred to the Ja...
Description: There are many mistakes in this article. The typesetting is not reasonable. Please read it critically. (Available when you have time) 1 four garbage collection algorithms PS Here we intro...