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)
?
1234567891011121314151617181920212223242526272829303132333435363738 |
import java.io.File;
import java.util.Arrays;
/*
* Copyright [2015] [Jeff Lee]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES 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
* 列出目录并排序
*/
public class DirListT {
public static void main(String[] args) {
// 获取当前目录
File path =
new File(
"."
);
// .表示当前目录
// 文件路径名数组
String list[] = path.list();
// 对String文件名进行排序
Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
// 打印
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
pathname
the parameter
null
is
-
In both, file implements the comparator interface to sort the filename.
static Comparator<String>
CASE_INSENSITIVE_ORDER
A String
Comparator that sorts objects, with the same effect 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
-
As below, the UML diagram of the file related core is given:
-
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 the 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:
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
import java.io.File;
/*
* Copyright [2015] [Jeff Lee]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES 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方法详细使用
*/
public class FileMethodsT {
private static void fileData(File f) {
System.out.println(
" 绝对路径:" + f.getAbsolutePath() +
"\n 可读:" + f.canRead() +
"\n 可写:" + f.canWrite() +
"\n 文件名:" + f.getName() +
"\n 上级目录:" + f.getParent() +
"\n 相对地址:" + f.getPath() +
"\n 长度:" + f.length() +
"\n 最近修改时间:" + f.lastModified()
);
if
(f.isFile())
System.out.println(
" 是一个文件"
);
else if
(f.isDirectory())
System.out.println(
" 是一个目录"
);
}
public static void main(String[] args) {
// 获取src目录
File file =
new File(
"src"
);
// file详细操作
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