Getresource (), getresourceasstream, and path issues)

Source: Internet
Author: User

It seems easy to get files using Java, but for many new people like me, I still have a very simple understanding, and I feel quite deep in use, which is usually the most frequently used, is to use the file class in Java. To obtain the C:/test.txt file, filefile = newfile ("C:/test.txt") will be used in this way, as we all know, it is hard-coded paths. In the Java spirit, applications should be built once and available everywhere. In terms of practical applications, the final generated application will also be deployed in an operating system outside windows. For Linux, if the C:/is used in the application, it will fail, we should try to avoid hard encoding, that is, using absolute paths directly.

 

In the servlet application, there is a getrealpath (stringstr) method. Although this method can also dynamically obtain the file path, it is not a secret to directly hand over the absolute path, but this is also a method that is not recommended, so what method can we better obtain files?

 

     That is class. getresource () and class. getresourceasstream () method, but many people still do not understand its usage, because many people (such as me not long ago) do not know what parameters should be passed to it. Of course, some people do not need to take care of them. Here, I will explain them to people who are not or are not familiar with them.

 

 

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 file and a build directory, which is the storage directory of the compiled java files (. class files, etc.)

How can we obtain

File1.txtFile2.txtFile3.txtWhat about the four file4.txt files?

 

First, let's talk about file3.txtand file4.txt.

File3.txt:

Method 1: file file3 = new file (test. Class. getresource ("file3.txt"). GetFile ());

Method 2: file file3 = newfile (test. Class. getresource ("/javaapplication/file3.txt"). GetFile ());

Method 3: file file3 = newfile (test. Class. getclassloader (). getresource ("javaapplication/file3.txt"). GetFile ());

 

File4.txt:

Method 1: file file4 = newfile (test. Class. getresource ("/file4.txt"). GetFile ());

Method 2: file file4 = newfile (test. Class. getclassloader (). getresource ("file4.txt"). GetFile ());

 

Good. We can have multiple options, but what about file1 and file2 files? How to get it?

The answer is: you can only write their absolute paths. They cannot be obtained using the class. getresource () method like file3 and file4. Their Obtaining Methods are as follows:

If the entire project directory is placed under C:/, file1 and file2 are obtained in the following ways:

File1.txt

Method 1: file file1 = new file ("C:/project/src/javaapplication/file1.txt ");

Method 2 :... No

 

File2.txt

Method 1: file file2 = new file ("C:/project/src/file2.txt ");

Method 2 :... No

 

To sum up, you want to get the file, and you have to generate it from the final one. class file is the starting point, do not use. the path of the Java file is the starting point, because it is actually used. class. java files are used, because Java is a compiled language.

 

As for the parameters of the getresouce () method, you can accurately locate the resource file based on the class and the relative path. As for its root directory, you use different idebuild is different location, but it is based on the top package as the root directory, for example, in the Web application, there is a WEB-INF directory, WEB-INF directory in addition to web. in addition to the XML file, there is a classes Directory, which is the top-level directory of your web application package. the root directory "/example of the class. For example, a file.txt file exists under the clasaesdirectory, and its relative path is"/file.txt ". If the relative path does not start with"/", it is relative. class path ..

 

There is also a getresourceasstream () method, the parameter is the same as the getresouce () method, it is equivalent to the result that you get the file with getresource (), and then the newinputstream (File) is the same

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.