writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
FaceBook: Bysocket
Twitter: Bysocket
Remember Java source code is set to start looking, wrote a series of related articles, received a good evaluation. Thank you, readers. I will still read the old writing to the old , and vivid image of the writing to experience. This is still a diagram , I study io this piece.
the main point of Java io–file should be
1. Cross-platform Problem solving
2, the security of the file
3, the document retrieval method
First, the introduction of small code
take a look at a simple demo: (PS: Open source project java-core-learning address :https://github.com/JeffLi1993 )
importjava.io.file;importjava.util.arrays;/* * Copyright [Jeff Lee] * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicab Le law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without WAR Ranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * */** * @author Jeff Lee * @since 2015-7-13 07:58:56 * List directories and sort */publicclassdirlistt {Publicstaticvoidmain (string[] A RGS) {//Get current directory File Path = NewFile ("."); /. Represents the current directory//file path an array group String list[] = Path.list (); Sorts a String file name Arrays.sort (List,string.case_insensitive_order); Print for (String Diritem: list) System.out.println (Diritem); }}
in Eclipse, right-click
run to get the following result:
, it is easy to notice that the names in their directories are sorted alphabetically and printed.
Let's review the API knowledge,
First , the constructor public File(String pathname)
creates a new File instance by converting the given pathname string to an abstract path name. If the given string is an empty string, the result is an empty abstract path name.
Parameters: Pathname – path name string
Thrown: NullPointerException – if the pathname parameter is null
in both, file implements the comparator interface to sort the filename.
Static Comparator < String >
case_insensitive_order
a Comparator that sorts a String object, with the same effect as comparetoignorecase.
Three, why would Path.list () return An array of string[] filenams? Why not List ?
self-answer: At this time, we should go to see the implementation of ArrayList, ArrayList is actually a dynamic array implementation. Dynamic, the disadvantage of dynamic is low efficiency . At this point, a fixed array is returned instead of a flexible class container because its directory element is fixed . The following is a comparison of ArrayList and array arrays:
Second, in-depth understanding of the source
How the File,file is formed. Along the source , you know that file has several important attributes :
1. Static Private FileSystem FS
FileSystem: Abstraction of the local file system
2. Path name of String path file
3. Inline Enumeration Class
pathstatus Address is valid enum class private static enum Pathstatus {INVALID, CHECKED};
4. Prefixlength prefix length
The UML diagram for the file-related core is given below:
in fact, the operation is FileSystem : The abstraction of the local file system, the real operation is the Filesytem of the derived class . Through the source ctrl+t found as follows:Win under the operation is win32filesystem and Winntfilesystem class. It seems that the file that really calls the system through jvm,native is them.
What about Linux ? So, a Linux version of the JDK, unzip, find Rt.jar. The Unixfilesystem class is then found in the Java/io directory. The truth!
so you can summarize the file operation source code such as call: In the middle of different JDK, is actually different class call native native method .
Third, the small demo to another
file is actually the same as what we see in the system. Just like we right-click Properties. You can see a lot of file information. Java file also has. Here is a detailed description of how a file is Related:
importjava.io.file;/* * Copyright [Jeff Lee] * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicab Le law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without WAR Ranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. * * */** * @author Jeff Lee * @since 2015-7-13 10:06:28 * File method verbose using */PUBLICCLASSFILEMETHODST {PRIVATESTATICVOIDFI Ledata (File f) {System.out.println ("absolute path:" + f.getabsolutepath () + "\ n readable:" + f.canread () + "\ n Writable:" + f.canwrite () + "\ n FileName:" + f.getname () + "\ n Parent Directory:" + f.getparent () + "\ n Relative Address:" + f.getpath () + "\ n Length: "+ f.length () +" \ n Last modified: "+ f.lastmodified ()); if (F.isfile ()) System.out.println ("is a file"); ElseIf (F.isdirectory ()) System.out.println ("is a Directory"); } publicstaticvoidmain (string[] args) {//Get src directory File File = newFile ("src"); File detailed operation FileData (file); }}
in Eclipse, right-click Run to get the following result: everyone should understand.
How does the file filter?
in the future, the filter involves the Fiter class.
Iv. Summary
1, see the source code is very simple, look at data structure. See how to Invoke. Or some kind of design pattern.
2, learn something, learn 1.1 deep point. It's too deep to be good enough.
3, Masons learn the code are on GitHub (synchronous osc git), welcome to Point Star, suggestions, progress together. Address: https://github.com/JeffLi1993
writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
FaceBook: Bysocket
Twitter: Bysocket
Diagram Java IO: First, File source code