Introduction to IDEA relative path

This article mainly introduces how to use relative paths correctly when using IDEA development tools, because I will also encounter problems in the process of using it. I hope to help my friends. If the writing is wrong, I also hope to point out the error. The place.

1. Use the reflection mechanism to obtain an instance of a class
String str = "com.myjava.test"
Class.forName(str);
In this, the class test.java is in the package com.myjava under the src . Note that it is not required Add the **.java** suffix and write directly to the class name

2. Use the class loader to get the file
class. class.getClassLoader().getResourceAsStream("config.properties")
At this time, the config.properties is directly under the src or
it can be a
class.getClassLoader().getResource("Images /1.png”)
1.png is a file under the package Images , this package is also directly under the src directory

3. New File(), and when using IO stream
File file = new File("src/Images/1.png")
FileInputStream fis = new FileInputStream("src/Images/1.png"); The
file is the same as above, but this Need to add the src directory, the use of IO stream is the same

Guess you like

Origin blog.csdn.net/qq_42599616/article/details/105254997