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 of Java, such
To obtain the C:/test.txt file, the file = new
File ("C:/test.txt"); in this case, I believe everyone knows that the path is hard-coded. in Java spirit, the application should be molded once, available everywhere, and
From the perspective of practical applications, the final generated applications will also be deployed in operating systems outside windows. For Linux, if C:/is used in an application, it will fail, we should
Avoid hard encoding, that is, directly using absolute paths.
In the servlet application, there is a getrealpath (string Str) 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.txt file2.txt file3.txt file4.txt what about these four 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 = new File (Test. class. getResource
("/Javaapplication/file3.txt"). getFile ());
Method 3: File file3 = new File (Test. class. getClassLoader (). getResource
("Javaapplication/file3.txt"). getFile ());
File4.txt:
Method 1: File file4 = new File (Test. class. getResource
("/File4.txt"). getFile ());
Method 2: File file4 = new File (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. You cannot use class. getResource like file3 and file4.
() The 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.
Same IDE
Build is different from the location, but are based on the top package as the root directory, for example, in the Web application, there is a WEB-INF directory, the WEB-INF directory
In addition to the web. xml file, there is a classes directory. That's right. It is the top-level directory of your WEB application package and the root directory of all. classes.
"/Example, if the clasaesdirectory contains a file.txt file, its relative path is"/file.txt ". If the relative path is not prefixed with"/", it is a phase
For the path of. class ..
There is also a getResourceAsStream () method, the parameter is the same as the getResouce () method, it is equivalent to using getResource
() Get the same result as the new InputStream (File) after obtaining the file.
Import Java. io. bufferedinputstream; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. pipedreader; <br/> Import Java. util. properties; </P> <p> public class databaseproperty {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) <br/>{< br/> New databaseproperty (); <br/> system. out. println (get ("jdbc_drive R "); <br/>}< br/> static private properties pro; <br/> Public databaseproperty () <br/>{< br/> pro = new properties (); <br/> try <br/> {<br/> // databaseproperty d = new databaseproperty (); <br/> // inputstream in = pipedreader. class. getresourceasstream ("DB. con "); <br/> inputstream in = getclass (). getresourceasstream ("DB. con "); <br/>/* <br/> * when using this getclass, please ensure that this configuration file exists in the class file directory !! */<Br/> // inputstream in = new bufferedinputstream (New fileinputstream ("DB. con "); <br/> pro. load (in); <br/> in. close (); <br/>}< br/> catch (ioexception E) <br/>{</P> <p >}< br/> Public static string get (string key) <br/>{< br/> return (string) Pro. getproperty (key); <br/>}< br/>}
The following is an example.
:
Simple Example: (Note: any class has a getClass () method) <br/> public class Test {<br/> public void getResource (String url) {<br/> InputStream stream = getClass (). getResourceAsStream (url); <br/> System. out. println (getClass (). getResource (url); <br/> byte [] line = new byte [1024*1024]; <br/> try {<br/> stream. read (line); <br/>}catch (IOException e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/ >}< Br/> String str = new String (line); <br/> System. out. println (str. trim (); <br/>}</p> <p> public static void main (String [] args) {<br/> new Test (). getResource ("/io/aaa.txt"); <br/>}< br/> running result: <br/> file:/D: /IBM/workspace/HtmlParser/io/aaa.txt <br/> abceefghijklmn <br/> the first line is the example of aaa.txt, and the second line is the content of aaa.txt. <Br/> note that the preceding "/" must exist, which indicates the project directory. Otherwise, the project directory cannot be found.