Illustration of Java IO: 1. File source code, iofile

Source: Internet
Author: User

Illustration of Java IO: 1. File source code, iofile

Writer: BYSocket)

Weibo: BYSocket

Bean flap: BYSocket

FaceBook: BYSocket

Twitter: BYSocket

I remember that the Java source code was first read by the set. I wrote a series of articles related to the set, which was well evaluated. Thank you. I will still read the old articles and write them vividly. This is still an illustration. I am studying IO.

The main point of Java IO-File should be

1. cross-platform solution

2. file security

3. File Retrieval Methods

I. Introduction of code

Please refer to a simple demo :( ps: open source project java-core-learning address: https://github.com/JeffLi1993)

?
1234567891011121314151617181920212223242526272829303132333435363738 importjava.io.File;importjava.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 * List and sort Directories */publicclass DirListT {    publicstatic void main(String[] args) {        // Obtain the current directory        File path = newFile(".");//. Indicates the current directory        // Array of file path names        String list[] = path.list();                 // Sort String file names        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 results:

It is easy to note that the names in the directory are sorted by letters and printed.

Let's review the API knowledge first,

First, the constructor public File (String pathname)

Creates a newFileInstance. If the given string is a null string, the result is a null abstract path name.

Parameters:
pathname-Path name string
Throw:
NullPointerException-If pathnameThe parameter is null

File implements the Comparator interface to sort the FileName.

static Comparator<String>
     CASE_INSENSITIVE_ORDER
A PairStringThe Comparator used to sort objects.compareToIgnoreCaseSame.

3. Why does path. list () return an array of String [] filenams? Why is it not a List?

Self-Answer: At this time, we should look at the implementation of ArrayList, which is actually a dynamic array implementation. Dynamic, the disadvantage of dynamic is low efficiency. In this case, a fixed array is returned, instead of a flexible class container, because its directory elements are fixed. The following is a comparison between ArrayList and Array:

Ii. deep understanding of source code

File. Following the source code, we know that File has several important attributes:

1. static private FileSystem fs

FileSystem: abstraction of the local file system

2. path Name of the String path File

3. inline enumeration class

Whether the PathStatus address is legal ENUM class private static enum PathStatus {INVALID, CHECKED };

4. prefixLength

The following shows the UML diagram of the core File:

In fact, the operation is FileSystem: the abstraction of the local file system, the real operation is the FileSytem derived class. Use the source code Ctrl + T to find the following: The Win32FileSystem and WinNTFileSystem classes are operated in Win. It seems that native calls the system's File through jvm.

What about Linux? Therefore, download JDK of Linux, decompress it, and find rt. jar. Then, the UnixFileSystem class is found in the java/io directory. The truth is clear!

Therefore, we can summarize the File operation source code called as follows: Different JDK classes actually call native methods on the local machine.

3. A new release of the demo

File is actually the same as the File we see in the system. Just like right-click the attribute. You can see a lot of File information. Java File is also available. The following describes how to create a file:

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 importjava.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 * Detailed use of the File Method */publicclass FileMethodsT {         privatestatic void fileData(File f) {        System.out.println(            "Absolute path :" + f.getAbsolutePath() +            "\ N readable :" + f.canRead() +            "\ N writable :" + f.canWrite() +            "\ N file name :" + f.getName() +            "\ N parent directory :" + f.getParent() +            "\ N relative address :" + f.getPath() +            "\ N length :" + f.length() +            "\ N last modification time :" + f.lastModified()            );        if(f.isFile())            System.out.println("Is a file");        elseif(f.isDirectory())            System.out.println("Is a directory");    }         publicstatic void main(String[] args) {        // Obtain the src directory        File file = newFile("src");        // Detailed file operations        fileData(file);    }}

In eclipse, right-click run to get the following results: you should understand it.

How to filter files?

Let's talk about it independently later. Filtering involves the Fiter class.

Iv. Summary

1. Reading the source code is very simple. Looking at the data structure. See how to call. Or some design patterns

2. Learn things, learn more than 1.1 points. Too deep. That's enough.

3. The Code learned by the bricklayer is on github (synchronized with osc git). Welcome to star, give comments, and make progress together. Address: https://github.com/JeffLi1993

Writer: BYSocket)

Weibo: BYSocket

Bean flap: BYSocket

FaceBook: BYSocket

Twitter: BYSocket

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.