When using the java.io, when java.lang.ClassLoader use it?
(Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understand the relative path
The detour! )
His summing up:
* java.io: Compared to the current user directory relative path read; focus on dealing with disk files or the use of pure java project.
(Although more common ClassLoader way, but if not JavaEE environment, to navigate to the classpath path indefinitely is unreasonable to read documents
Of. )
* java.lang.ClassLoader: compared with the classpath relative path read; proposed JavaEE environment using this approach.
Collate information 1: http://www.code168.com/bbs/html/2005-12-9/23554625833.html
Ask:
java package jar labeled follow-up questions! ! ! ! How to read the jar inside the package configuration file?
Answer 1:
If java.util.ResourceBundle not have to worry about what it would have from the class loader folder / jar file to find
properties file.
If you have noticed, java There are two ways to check documents, java.util.io and two java.lang.ClassLoader.
java.io:
File file = new File ("...");
FileInputStream fis = new FileInputStream ("...");
FileReader fr = new FileReader ("...");
ClassLoader:
ClassLoader loader = XXXClass.class.getClassLoader ();
or
ClassLoader loader = Thread.currentThread (). GetContextClassLoader ();
URL url = loader.getResource ("...");
File file = new File (url.getFile ());
InputStream input = loader.getResourceAsStream ("...");
The two, a project loader folder are from the taking, a class loader folder are from the taking, class loader
including the jar file folder.
I think you should get the point? Writing test their own programs click on the know.
Answer 2:
File file = new File (url.getFile ()); not in your situation.
Now that the general situation has been getting ClassLoader resource, there is no need to overdo and then into a File.
File conversion is in fact in order to get absolute path, we come across such a situation.
A Web application, using tomcat startup, tomcat will build an application folder, folder below have a web-inf
folder, and then the following are classes directory, classes directory below are all the java classes. procedures necessary to use a property file
Record data, can only be used io package positioning to an absolute path, using class loader can be a relative path, regardless of tomcat in the electricity customers
Brain at any location, but if Writing classes folder in the following documents, tomcat will reload web server, the page will be overloaded
. In order to locate the application folder, and web-inf tied, first classloader, then into a file to get the whole path, removed after
Noodles do not need the folder, you can get tomecat built web application's absolute path.
With your situation is different, classloader locate files, not jar inside. Java io use in my opinion can not be set inside the jar
.
Collate information on the Second:
Java path problem final solution
http://www.matrix.org.cn/thread.shtml?topicId=6d0bbeed-9157-11db-ab77-
2bbe780ebfbf & forumId = 19
First, when compared to the current user directory relative path
Is compared with the System.getProperty ( "user.dir") return path.
For the average project, This is the project root path. For JavaEE server, this may be a server path. This does not have a unified
Norm!
Therefore, never use "vis-à-vis the current user directory relative path." However:
By default, java.io package type is always based on the current user directory to analyze the relative path name. In this directory by the system property user.dir
Designation, is usually called Java virtual machine directory.
That is to say, in the use of java.io package classes, it is best not to use relative path. Otherwise, although in J2SE applications may still
Normal, but the J2EE process, we would just go wrong! Moreover, the path, in a different server is different!
Second, compared to the relative path classpath
Such as: compared with the
file: / D: / java/eclipse32/workspace/jbpmtest3/bin / relative path to this path. One of, bin is the project
classpath. All the Java source file compiled. Class files are copied to this directory.
Three, the relative path of best practices
Recommend the use of vis-à-vis the current classpath relative path
Therefore, we use the relative path should be used when compared with the current classpath relative path.
ClassLoader Class getResource (String name), getResourceAsStream (String name) and other methods, when used in relation to
Before the project classpath relative path to search resources.
Property file used to read the ResourceBundle class getBundle (String path) is also true.
By viewing the ClassLoader class and its related source code, I found that it was in fact used the URI form of absolute path. Through a
Classpath of the current form of absolute URI path, relative path constructed forms of the URI absolute path. (This in fact is a guess,
Since the internal JDK call the SUN source code, which code do not belong to JDK, not open source. )
Four, the relative path or absolute path is essentially
Therefore, in the final analysis, Java essence can only use the absolute path to look for resources. All looking for the relative path resources are nothing more
Some convenient way. However, API are in the bottom to help us build the absolute path in order to find the resources!
Source article: http://www.diybl.com/course/3_program/java/javajs/2008224/100821.html







