"JAva in Class.getResource usage (for configuration file reads)"
Using Java to get the file, listen like simple, but for many new people like me, or master quite shallow, with a feeling quite deep, often the most often used, is to use the Java file class, if you want to get c:/test.txt files, this will be the case with "C :/test.txt "); So what is the problem, I believe everyone knows, is the path hard code, for the Java spirit, the application should be one-time molding, everywhere, and from a real-world application, the resulting application will be deployed to the operating system outside of Windows, for Linux, In the application of c:/such words, is the failure, so we should try to avoid the use of hard-coded, that is, the direct use of absolute path.
In the servlet application, there is a getrealpath (String str) method, although it is also possible to dynamically obtain the path of the file, not the direct handwritten absolute path, but this is not recommended to use the method, then, we have a way to better get the file?
That is the Class.getResource () and the Class.getresourceasstream () method, but many people still do not understand its usage, because many people (like me recently) do not know how to pass the parameters to it, of course, Some people have durable as the fire is pure green, these people do not need to take care of, here only to the person who is not or is not very familiar to explain a little.
For example, we have the following directory
|--project
|--src
|--javaapplication
|--test.java
|--file1.txt
|--file2.txt
|--build
|--javaapplication
|--test.class
|--file3.txt
|--file4.txt
In the above directory, there is a src directory, which is the directory of the Java source files, there is a build directory, this is the Java compiled files (. class files, etc.) storage directory
So how do we get in the test class separately
File1.txt File2.txt file3.txt file4.txt these four files?
First talk about File3.txt and File4.txt
File3.txt:
Method One: File File3 = new file (Test.class.getResource ("File3.txt"). GetFile ());
Method Two: File File3 = new file (Test.class.getResource ("/javaapplication/file3.txt"). GetFile ());
Method Three: File File3 = new file (Test.class.getClassLoader (). GetResource ("Javaapplication/file3.txt"). GetFile ());
File4.txt:
Method One: File File4 = new file (Test.class.getResource ("/file4.txt"). GetFile ());
Method Two: File File4 = new file (Test.class.getClassLoader (). GetResource ("File4.txt"). GetFile ());
Well, we can have a variety of options, but what about file1 and file2 files? How do I get it?
The answer is, you can only write their absolute path, not like File3 and file4 in the same way as the Class.getResource (), they get the method as follows
If the entire project directory is placed under c:/, then the File1 and File2 methods are
File1.txt
Method One: File File1 = new file ("C:/project/src/javaapplication/file1.txt");
Method Two: ... No
File2.txt
Method One: File File2 = new file ("C:/project/src/file2.txt");
Method Two: ... Also no
To summarize, that is, you want to get the file, you have to start from the resulting. class file, do not take the path of the. java file as the starting point, because the real use of the. class, will not take a. java file is used, because Java is a compiled language
As for the Getresouce () method parameters, you take the class as the starting point, and then combined with the concept of relative path, you can accurately locate the resource file, as for its root directory, you use different IDE build out is different location, but all with the top-level package as the root directory, For example, in a Web application, there is a web-inf directory, Web-inf directory in addition to the Web. xml file, there is a classes directory, yes, it is the top-level directory of the package for you, and all the. Class root directory "/", If the Clasaes directory has a file.txt file, its relative path is "/file.txt", if the relative path does not start with "/", then it is the path relative to the. class.
There is also a getResourceAsStream () method, which is the same as the Getresouce () method, which is equivalent to the same result as when you get the file with GetResource () and then new InputStream (file)
Windows absolute Path:
Start with a drive letter such as c:/a.txt Windows relative path:. Refers to the current directory: Refers to the top-level directory of the current directory./test indicates that the test folder under the current directory/test represents the test folder under the current drive letter Www.2cto.comLinux Absolute path:
root directory/starting path such as/indicates the root root directory of the Linux relative path:./refers to the current directory. /refers to the top-level directory of the current directory. Represents the current directory,.. Representing the parent directory seems to be unified.
When you get a configuration file such as a. properties file in Windows or Linux
The values returned when using Class.getResource or Classloader.getresource to obtain an absolute path classpath are different
| 1 |
String basepath=Thread.currentThread().getContextClassLoader().getResource("").toString(); |
Windows output
| 1 |
basepath=file:/D:/Workspaces/edu.bizoss.com/WebRoot/WEB-INF/classes/ |
Linux output
| 1 |
basepath=file:/Workspaces/edu.bizoss.com/WebRoot/WEB-INF/classes/ |
So it's different to get the absolute path intercept.
| 12345 |
if (System.getproperty ( "File.separator" ). equals ( return basepath.substring (6, Basepath.length ()); } else { Code class= "CSharp keyword" >return basepath.substring (5,basepath.length ()); |
"Java" "Path"