Java Get resource file

Source: Internet
Author: User

For example, we have the following directory
|--project
|--src
|--javaapplication
|--test.java
|--file1.txt
|--file2.txt
|--build
|--javaapplication
& NBSP;           |--TEST.CLASS 

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.txtfile4.txt These four files?

First talk about File3.txt and File4.txt
File3.txt:
Method One: File file3 = NewFile (Test.class.getResource ("File3.txt"). GetFile ());
Method Two: File file3 = NewFile (Test.class.getResource ("/javaapplication/file3.txt"). GetFile ());
Method Three: File file3 = NewFile (Test.class.getClassLoader (). GetResource ("Javaapplication/file3.txt"). GetFile ());

File4.txt:
Method One: File file4 = NewFile (Test.class.getResource ("/file4.txt"). GetFile ());
Method Two: File file4 = NewFile (Test.class.getClassLoader (). GetResource ("File4.txt"). GetFile ());

But File1 and file2 filescan only write their absolute path, not like File3 and file4 in the same way as the Class.getResource () This method, they get the method as follows(Assuming the entire project directory is placed under c:/)
File1.txt
File file1 = NewFile ("C:/project/src/javaapplication/file1.txt");

File2.txt
File file2 = NewFile ("C:/project/src/file2.txt");

To summarize, you want to get the file, you have to start with the resulting. class file, not the path of the. java file as the starting point, because the real use of. Class, will not take a. java file is used.

Java Get resource file

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.