Writer :BYSocket(泥沙砖瓦浆木匠)
微 博:BYSocket
豆 瓣:BYSocket
FaceBook:BYSocket
Twitter :BYSocket
记得Java源码是集合开始看的,写了一系列集合相关的文章,受到不错的评价。感谢各位读者。我依旧会读到老写到老,并生动形象的写出来心得体会。这次依旧是图解,我研究IO这块。
Java IO – File的要点,应该是
1、跨平台问题的解决
2、文件的安全
3、文件的检索方法
代请看一个简单的小demo:(ps:开源项目java-core-learning地址:https://github.com/JeffLi1993)
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
 * 列出目录并排序
 */
publicclassDirListT {
    publicstaticvoidmain(String[] args) {
        // 获取当前目录
        File path = newFile(".");// .表示当前目录
        // 文件路径名数组
        String list[] = path.list();
         
        // 对String文件名进行排序
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
         
        // 打印
        for(String dirItem : list)
            System.out.println(dirItem);
    }
} 
 
 
如图,很容易注意到了,其目录下的名字排序按字母并打印了。
先回顾下API知识吧,
首先构造函数 public File(String pathname)
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。如果给定字符串是空字符串,那么结果是空抽象路径名。
参数: pathname – 路径名字符串 抛出: NullPointerException – 如果 pathname 参数为 null
二者,File实现了Comparator接口,以便对FileName进行排序。
static Comparator<String>
CASE_INSENSITIVE_ORDER
一个对 String 对象进行排序的 Comparator,作用与 compareToIgnoreCase 相同。
三者, path.list()为什么会返回String[] filenams的数组呢?怎么不是List呢?
自问自答:这时候,我们应该去看看ArrayList的实现,ArrayList其实是动态的数组实现。动态,动态的弊端就是效率低。此时,返回一个固定的数组,而不是一个灵活的类容器,因为其目录元素是固定的。下面是ArrayList和数组Array的比较:
File,File究竟是怎么构成的。顺着源码,知道了File有几个重要的属性:
1、static private FileSystem fs
FileSystem : 对本地文件系统的抽象
2、String path 文件路径名
3、内联枚举类
PathStatus 地址是否合法 ENUM类 private static enum PathStatus { INVALID, CHECKED };
4、prefixLength 前缀长度
如下,给出File相关核心的UML图:
其实操作的是 FileSystem : 对本地文件系统的抽象,真正操作的是 FileSytem的派生类。通过源码Ctrl+T发现如下:Win下操作的是 Win32FileSystem 和 WinNTFileSystem类。看来真正通过jvm,native调用系统的File是他们。
那Linux呢?因此,下了个Linux版本的JDK,解压,找到rt.jar。然后java/io目录中,找到了UnixFileSystem类。真相大白了!
所以可以小结File操作源码这样调用的:中间不同JDK,其实是不同的类调用本机native方法。
File 其实和我们在系统中看的的文件一样。就像我们右键,属性。可以看到很多File的信息。Java File也有。下面是一个文件的相关方法详情:
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
 * File方法详细使用
 */
publicclassFileMethodsT {
     
    privatestaticvoidfileData(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(" 是一个文件");
        elseif(f.isDirectory())
            System.out.println(" 是一个目录");
    }
     
    publicstaticvoidmain(String[] args) {
        // 获取src目录
        File file = newFile("src");
        // file详细操作
        fileData(file);
    }
} 
 
 
文件如何过滤呢?
以后独立讲吧,过滤涉及Fiter类。
1、看源码很简单,看数据结构。看如何调用。或者是有些设计模式
2、学东西,学一点一点深一点。太深不好,一点就够了
3、泥瓦匠学习的代码都在github上(同步osc git),欢迎大家点star,提意见,一起进步。地址:https://github.com/JeffLi1993
Writer :BYSocket(泥沙砖瓦浆木匠)
微 博:BYSocket
豆 瓣:BYSocket
FaceBook:BYSocket
Twitter :BYSocket
原文:http://my.oschina.net/jeffli1993/blog/478690