java File详解
2015-09-17 20:26
302 查看
一、简介
File类是“文件”和“目录名”的抽象表示形式。因此在java语言中,File类既可以表示文件也可以表示目录。尽管java.io定义的大多数类是实行流式操作的,而File类则不是,它没有指定信息怎样从文件读取或向文件存储。File描述了文件本身的属性,直接处理文件和文件系统,是唯一一个与文件本身有关的操作。
1.1File的继承与实现接口
File直接继承于Object,实现了Serializable接口和Comparable接口。[code]publicclassFileimplementsSerializable,Comparable<File>
[/code]
File类实现Serializable接口,意味着File对象支持序列化操作。
File类实现Comparable接口,意味着File对象之间可以比较大小,此外File能直接被存储在有序集合(如TreeSet、TreeMap中)。
结构图如下:
1.2File使用注意事项
1、创建File类对象时要注意的地方
首先我们要区分两个名词:“绝对路径”和“相对路径”。绝对路径:即完整的路径,比如:D:\MyCode\Java\FileDemo
相对路径:会在代码所在的地方生成,比如:res/jay.txt就会在你代码的目录下成jay.txt文件
另外,由于路径名字符或者是抽象路径名的转换与生俱来就是依赖于系统的。比如在Unix系统中,绝对路径名的前缀总是“/”,然而在Windows系统中,绝对路径名的前缀总是”\”。
关于前缀的解决办法:
例1:在Windows中,路劲名为D:\ProgramFiles
在java程序中,必须将”\”转换为”/”或者”\\”,比如:D:/ProgramFiles或者D:\\ProgramFiles
如果想让程序跨平台的话,由于Linux中是”/”,而Windows中是”\”,我们可以使用File提供的常量字符串File.separator,它能根据系统平台的不同,自动转换成”/”还是”\\”。
1.3File的常见方法
1)判断文件或者目录是否存在:exists()2)判断是否为目录:isDirectory()
3)判断是否为文件:isFile()
4)判断文件是否可读:canRead()
5)判断文件是否可写:canWrite()
6)判断文件是否隐藏:isHidden()
7)判断文件路径是否为绝对路径:isAbsolute()
8)判断文件路径是否相同:equals():返回true,false
compareTo():是否相等,相等返回0,小于返回负数,大于返回正数
9)获取文件的绝对路径:getAbsolutePath()
10)获取文件名称:getName()
11)获取文件大小:length()
12)获取文件最后被修改的时间:lastModified()
13)获取文件的路径:getPath()
14)获取文件的上层文件的目录:getParent()
15)创建一个新文件:createNewFile()
16)创建一个新目录:mkdir()
17)删除文件或目录:delete()
18)修改文件的名称:renameTo(str)
19)修改文件为只读:setReadOnly()
20)修改最后的修改时间:setLastModified(longtime)
二、源码分析
2.1成员变量
所有成员变量如下:下面列举一些比较关键的成员变量
//TheFileSystemobjectrepresentingtheplatform'slocalfilesystem. //获取本地文件系统 privatestaticfinalFileSystemfs=DefaultFileSystem.getFileSystem(); //文件路径名 privatefinalStringpath; //标记文件路径是否无效 privatetransientPathStatusstatus=null; //Thelengthofthisabstractpathname'sprefix,orzeroifithasnoprefix. privatefinaltransientintprefixLength; /** *Thesystem-dependentdefaultname-separatorcharacter.Thisfieldis *initializedtocontainthefirstcharacterofthevalueofthesystem *property<code>file.separator</code>.OnUNIXsystemsthevalueofthis *fieldis<code>'/'</code>;onMicrosoftWindowssystemsitis<code>'\\'</code>. * */ publicstaticfinalcharseparatorChar=fs.getSeparator(); //Thesystem-dependentpath-separatorcharacter,representedasastringforconvenience. publicstaticfinalStringpathSeparator=""+pathSeparatorChar;
1、成员变量fs是如何获取到本地文件系统的呢?
在Windows平台下,所下载的JDK中,我们找到了WinNTFileSystem文件系统
那Linux呢?因此,下了个Linux版本的JDK,解压,找到rt.jar。然后java/io目录中,找到了UnixFileSystem类。真相大白了!
其实是根据不同的平台,安装不同版本的JDK,根据平台下对应版本的JDK,加载本地文件系统。
解决了fs这个成员变量,那么separatorChar这个成员变量就迎刃而解了。
2.2构造函数
1、首先看看整体如下:2、挑选比较有代表性的方法进行讲解
1)File(Stringpathname)
[code]publicFile(Stringpathname){ if(pathname==null){ thrownewNullPointerException(); } this.path=fs.normalize(pathname); this.prefixLength=fs.prefixLength(this.path); }
该构造方法主要是初始化了path、prefixLength两个成员变量,其中path通过WinNTFileSystem中normalize公开方法将其规范化。[/code]
规范化后的好处,比如
publicstaticvoidmain(String[]args) { Filefile=newFile("d:/test.txt"); System.out.println(file.getPath()); } File:
[code]publicStringgetPath(){ returnpath; }
[/code]
输出结果:d:\test.txt
主要我们输入的是pathname="d:/test.txt"然而path得到的是"d:\test.txt”。
2)File(Fileparent,Stringchild)
publicFile(Fileparent,Stringchild){ if(child==null){ thrownewNullPointerException(); } if(parent!=null){ if(parent.path.equals("")){ this.path=fs.resolve(fs.getDefaultParent(), fs.normalize(child)); }else{ this.path=fs.resolve(parent.path, fs.normalize(child)); } }else{ this.path=fs.normalize(child); } this.prefixLength=fs.prefixLength(this.path); }
其目的还是为path、prefixLength两个成员变量赋值。
其中,fs.resolve(parent.path,fs.normalize(child));目的就是在合理范围内拼接父与子路径
2.3创建操作
createNewFilecreateTempFile
mkdir
1)createNewFile()
publicbooleancreateNewFile()throwsIOException{
//1、检查是否有权限对该文件进行操作
SecurityManagersecurity=System.getSecurityManager();
if(security!=null)security.checkWrite(path);
//2、检查文件路径是否有效
if(isInvalid()){
thrownewIOException("Invalidfilepath");
}
//3、在Windows中,调用WinNTFileSystem中createFileExclusively方法,该方法是调用系统底层方法。
returnfs.createFileExclusively(path);
}
2)mkdir()
[code]//此方法创建此抽象路径名的目录。 publicbooleanmkdir(){ SecurityManagersecurity=System.getSecurityManager(); if(security!=null){ security.checkWrite(path); } if(isInvalid()){ returnfalse; } returnfs.createDirectory(this); }
[/code]
2.4删除操作
deletedeleteOnExit
1)delete()
publicbooleandelete(){
//1、权限检查
SecurityManagersecurity=System.getSecurityManager();
if(security!=null){
security.checkDelete(path);
}
//2、检查文件路径是否有效
if(isInvalid()){
returnfalse;
}
//3、调用系统本地底层删除方法,
//不过在删除之前做了一些清理,cache.clear();prefixCache.clear();
returnfs.delete(this);
}
而deleteOnExit一般用在删除临时文件。
注意:file.delete()或file.deleteOnExit()方法只能删除文件或空文件夹
2.5获取文件操作
1)getAbsoluteFile()
//Returnstheabsoluteformofthisabstractpathname.EquivalenttonewFile(this.getAbsolutePath()). publicFilegetAbsoluteFile(){ StringabsPath=getAbsolutePath(); returnnewFile(absPath,fs.prefixLength(absPath)); }
2)getCanonicalFile()
//Returnsthecanonicalformofthisabstractpathname.EquivalenttonewFile(this.getCanonicalPath()). publicFilegetCanonicalFile()throwsIOException{ StringcanonPath=getCanonicalPath(); returnnewFile(canonPath,fs.prefixLength(canonPath)); }
2.6获取路径操作
1)getAbsolutePath()
//此方法返回此抽象路径名的绝对路径名字符串。 publicStringgetAbsolutePath(){ returnfs.resolve(this); }
2)getCanonicalPath()
//此方法返回此抽象路径名的规范路径名字符串。 publicStringgetCanonicalPath()throwsIOException{ if(isInvalid()){ thrownewIOException("Invalidfilepath"); } returnfs.canonicalize(fs.resolve(this)); }
3)getPath()
[code]//此方法此抽象路径名转换为一个路径名字符串。 //Convertsthisabstractpathnameintoapathnamestring //此处pathname与File(Stringpathname)中pathname一致 publicStringgetPath(){ returnpath; }
4)getParent()[/code]
//此方法返回此抽象路径名的父路径名的字符串,或者null,如果此路径名没有指定父目录。 publicStringgetParent(){ //查找最后分隔符的位置 intindex=path.lastIndexOf(separatorChar); if(index<prefixLength){ if((prefixLength>0)&&(path.length()>prefixLength)) returnpath.substring(0,prefixLength); returnnull; } //获取最后分隔符之前的所有字符 returnpath.substring(0,index); }
由于File中的源码基本上调用系统底层的API,故不再介绍。
参考:
1、
2、
3、
相关文章推荐
- Java的开始?
- spring2.5 xml的简单入门示例(aop)
- java中的随机数Random
- Java 泛型、通配符? 解惑
- eclipse 如何修改jsp的编码方式?
- Java乱码处理
- java.io.IOException: Prepare failed.: status=0x1
- java 堆中新生代老年代
- Struts2的类型转换器
- Struts2的类型转换器
- java笔记——面向对象异常
- 配置Spring数据源
- java中如何获取一个正整数的位数?
- ajax上传文件struts2后台接收处理
- JavaBean中DAO设计模式简介
- SSH-Struts(三)—ActionForm
- 赛马网ACM试题(原杭电oj ACM)java版答案(1000,10001,1002)
- 基于Maven的SpringMVC+Mybatis+mysql的框架搭建
- Java的String中的subString()方法
- struts2 开发流程以及配置文件