Locate the workspace folder that Eclipse uses to save the Java project.
Open a Java project, which opens the Heima project, and finds that there are some directories and files in it.
One of them has a. classpath file. So, what is classpath for? What is written inside.
With the calculation of this opened to see:
I don't know what it means. On the Internet to find some information:
<!--source directory-->
<classpathentry kind= "src" path= "src"/>
<!--JDK Runtime container-->
<classpathentry kind= "Con" path= "Org.eclipse.jdt.launching.JRE_CONTAINER"/>
<!--The following is a directory for you to customize for Class library path-->
<classpathentry kind= "Lib" path= "Lib/swing-layout-1.0.3.jar"/>
<!--compiler Output class directory-->
<classpathentry kind= "Output" path= "Bin"/>
So: The SRC directory is the directory where the source code resides, and the bin is the directory where the class bytecode files are stored.
----------------------
I'll change the. classpath file:
Change path to "bi" in <classpathentry kind= "Output" path= "Bin"/>
Modify. classpath file changes to restart Eclipse
Run: Copytext.java
After run: Error: The main class Day18 cannot be found or can not be loaded. CopyText
Solution: Read the following analysis, as long as the original bin directory under the Day18 directory copy to the BI folder can be.
------------------
After you modify the. classpath file, restart Eclipse and create a new black bag under engineering src
You will see a new folder bi in the engineering directory, and a black folder under the Bi folder
The Black folder is empty.
--------------------
Now create a new Test.java in Eclipse's Heima project black bag.
public class Test {public
static void Main (string[] args) {
System.out.println ("Hello");
}
Then save, you'll find the Test.class file under the Black folder.
---------------
Compile and run Test.java program output in eclipse:
---------------
Create a new Eula.txt file under the Black folder and load it with the class loader
Test.class.getClassLoader (). getResourceAsStream ("Black/eula.txt");
InputStream is = Test.class.getResourceAsStream ("Eula.txt");
FileWriter FW = New FileWriter ("Mm.txt");
int ch = 0;
while ((Ch=is.read ())!=-1) {
fw.write (CH);
Fw.flush ();
}
Finally, a mm.txt file is generated under the Heima project.
So it is known that the phase path of IO flow is engineering.
The class loader relative loading path is the directory that holds the class bytecode file specified in the. classpath.
The directory that holds the class bytecode file specified in MyEclipse. Classpath is webroot/web-inf/classes
<classpathentry kind= "Output" path= "Webroot/web-inf/classes"/>
Therefore, when the class loader is used to load the file in MyEclipse, it will also be loaded relative to the Webroot/web-inf/classes directory.
-------------------set build Path in eclipse
Window->preferences->java->build Path
In Eclipse, set up the directory where the source code resides and the directory that holds the class bytecode file. When a new project is built, the project begins to take effect and the. classpath file for that project is also corresponding.
------------------------------
Application: Create the HashSet instance object with the method of reflection in the configuration file, observe the running result.
The contents of the configuration file config.properties are:
Classname=java.util.hashset
1, using the traditional IO stream to read the configuration file
Import java.io.*;
Import java.util.*;
public class Propsload {public
static void Main (string[] args) throws Exception {
InputStream ips = new FileInput Stream ("Config.properties");//io stream read configuration file
Properties props = new properties ();
Props.load (IPS);
String className = Props.getproperty ("ClassName");
Collection collections = (Collection) class.forname (className). newinstance ();//Reflection
reflectpoint pt1 = new Reflectpoint (3,3);
Reflectpoint pt2 = new Reflectpoint (5,5);
Reflectpoint PT3 = new Reflectpoint (3,3);
Collections.add (PT1);
Collections.add (PT2);
Collections.add (PT3);
Collections.add (PT1);
System.out.println (Collections.size ());
}
In Eclipse, if you put the config.properties file in the same package as Propsload.java or the SRC directory, run:
Exception in thread "main" java.io.FileNotFoundException:config.properties (the system cannot find the specified file.) )
So how to put it.
For IO streams, use a relative path to write the "config.properties" path directly, and the Config.properties file should be placed in your engineering directory.
2, use the class loader to load the configuration file.
Now put the config.properties file in the engineering directory
Import java.io.*;
Import java.util.Collection;
Import java.util.Properties;
public class Classload {public
static void Main (string[] args) throws Exception {
InputStream ips= ClassLoad.class.getClassLoader (). getResourceAsStream ("config.properties")//loading resource file using the ClassLoader
properties props = new Properties ();
Props.load (IPS);
Ips.close ();
String className = Props.getproperty ("ClassName");
Collection collections = (Collection) class.forname (className). newinstance ();
Collection collections = new HashSet ();
Reflectpoint pt1 = new Reflectpoint (3,3);
Reflectpoint pt2 = new Reflectpoint (5,5);
Reflectpoint PT3 = new Reflectpoint (3,3);
Collections.add (PT1);
Collections.add (PT2);
Collections.add (PT3);
Collections.add (PT1);
System.out.println (Collections.size ());
}
Run result: Exception in thread "main" java.lang.NullPointerException
A null pointer exception occurs because the IPs reference is null, stating that the class loader did not find the configuration file, that the path provided was incorrect, or that the config.properties file was stored in a wrong path.
----------------
Now you need to know which directory the ClassLoader load file defaults to.
It's time to look at Eclipse, the project's. classpath file.
See this. classpath file <classpathentry kind= "Output" path= "Bin"/>
You know, the class loader loads the file by default relative to bin this directory. Therefore, the config.properties file should be stored in the bin directory. It's OK to run it again.
"Class loader to find the path of the class file (the bin directory to store the class file, Bin is the. classpath file Set)"
------------------
The InputStream getresourceasstream (String name) method is also provided in the API of the Class class.
With a class to load, rather than the classloader, in fact, internal or call getClassLoader
You can then write to load the resource file:
InputStream ips= ClassLoad.class.getResourceAsStream ("config.properties");
What's the benefit of using class loading?
The class class provides this getResourceAsStream method is very cool, only need to put the configuration file and the source program in the same package can be.
If developed under Eclipse, the configuration file is placed under the SRC directory or under a subdirectory of SRC, which is automatically copied to the bin directory or the bin directory corresponding to the SRC directory.
(The bin directory and the SRC directory are corresponding)
------------
You can use a class to load, either with a relative path or with an absolute path. As long as the Da Yu "/" is not relative, but relative to the classpath root (classpath root is bin,/on behalf of bin).
Classload.java is the source program under the Cn.itcast.enhance package. and Config.properties and Classload.java in the same bag.
This time using an absolute path to successfully load the words. Start by writing the full path from the root "/":
InputStream ips= ClassLoad.class.getResourceAsStream ("/cn/itcas/enhance/config.properties");
If the resource file is related to my package, I use a relative path. If it doesn't matter with my bag, use the absolute path.
Whether it is relative or absolute, the internal call is getClassLoader.