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