Read getResourceAsStream in java

I summary

When reading the configuration file, you will always struggle with the problem of path. In fact, all path problems can't escape a few keywords

Relative path, absolute path, classpath

First, let's introduce these paths

Relative path:

Relative path: as the name suggests, the path is relative. For example, the current path is test,

If you want to locate the file under test, you can use the relative path,

For example, there is aaa under test,

Then the path of aaa relative to test is test/aaa

Absolute path:

Absolute path is also a "relative path", but it is relative to the drive letter (c drive, d drive waiting) or the root directory in linux

For example: C:\Users894\Desktop\CSDN notes

Classpath:

As the name suggests, this class path is the path to the class file. Why do you use a class path in java?

It must be for convenience. How can it be convenient?

For example, I wrote a project in which there are many source files. After compiling the source files, many class files will be generated

You can't mix these java files with class files. It's so messy,

As like as two peas, ide creates a folder to store class files, also called output path output, which stores the same class store files as the source file directory results.

That's the problem. After talking so much, does it have anything to do with the classpath?

Let's think about it. If there is no classpath, there are only relative paths and absolute paths

If you need to locate a file, the absolute path must not work, because the project must be migrated

You can only use the relative path. If you use the relative path, it is better to be relative to the root directory of the project, because you can find all the files

Is it troublesome to write the relative path of the project every time you want to read the file

Therefore, the classpath is introduced, which is actually the relative path of the project, but is written to a file In classpath

Then, when a java program runs, as long as the relative path is used, it will start from this Find the relative path to in the classpath file

II Usage of getResourceAsStream

1. General

This method is generally used to obtain the information of the configuration file

2. Usage

Two ways of use:

1.TestClass.class.getResourceAsStream(String path);

2.TestClass.class.getClassLoader.getResourceAsStream(String path);

3.class.getResourceAsStream

If the path does not contain "/", the file is found from the path of the current class file

If the path takes "/", it is from the classpath Find the file in classpath

4.class.getClassLoader.getResourceAsStream

This path cannot contain /, because it means to find files in the classpath

5. Detailed explanation of classpath in maven

<?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:

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-ziSJvsbJ-1626164554225)(D:\blogPic\image-20210713135652524.png)]

In maven project, the configuration files are generally placed in resources, and the configuration files in this folder are generally copied to classes and test classes under the target path

In the classpath, there are configurations pointing to target/classes and target / test classes respectively. Then getResourceAsStream can find the corresponding file according to this xml file

III Test code

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 the conversion stream to solve the character encoding problem
		InputStreamReader isr = new InputStreamReader(is);
		//Get properties object
		Properties properties = new Properties();
		//Load resource file to properties
		properties.load(isr);
		//Close the stream (it can be closed after loading)
		isr.close();
		//Gets the properties in the configuration file
		String username = properties.getProperty("username");
		System.out.println(username);
		
		
	}
}

You can use this class to test the use of getResourceAsStream

Keywords: JavaSE

Added by rationalrabbit on Sat, 22 Jan 2022 09:47:02 +0200