Java.io.File类的学习
来源:互联网 发布:手机壳品牌排行榜知乎 编辑:程序博客网 时间:2024/05/29 05:02
File对象的理解
File 的实例对象并不是和磁盘中的文件(或者文件夹)一一对应的。只有File 对象调用exists()
,并且返回了true
,该 File 对象才和磁盘中的某个真实的文件有映射关系。
换个角度思考,在java.nio.file
中,并没有使用 File 作为文件系统单位元素(文件或者文件夹为文件系统的单位元素)的命名,而是使用了 Path。顾名思义,它就是一个路径,并不是代表一个真实存在的文件。
File类中的属性
pathSeparator与separator的区别?
pathSeparator:路径分隔符,比如在 Windows 的PATH
环境变量中,不同文件路径使用“;”进行分割,这个分隔符就是 pathSeparator。
separator:分隔符,比如在一个文件路径中,使用“\”进行分割不同的文件层次,这个分隔符就是separator。
from stackoverflow
If you mean File.separator and File.pathSeparator then:
File.pathSeparator
is used to separate individual file paths in a list of file paths. Consider on windows, thePATH
environment variable. You use a ; to separate the file paths so on WindowsFile.pathSeparator would be;
.File.separator
is either/
or\
that is used to split up the path to a specific file. For example on Windows it is\
orC:\Documents\Test
File类对象的构造
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string. File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. File(String parent, String child)
File类中的方法
方法可以分为:针对文件属性、针对文件本身、获得文件夹中文件、获得磁盘空间文件。
文件属性
文件属性的获取:
- 可读可写可执行、是否是隐藏文件、上次修改时间
- exists()、isDirectory()、isFile()
文件属性的修改
可读可写可执行、上次修改时间
文件本身的操作
- 路径
- 比较:compareTo(File pathname)
- 获取:路径、文件名、父文件(路径)
文件基本操作
创建新文件:
boolean createNewFile()
创建一个新的文件,如果文件已经存在了,返回false。
该操作是一个原子操作。
创建临时文件
static File createTempFile(String prefix, String suffix)
会调用 createTempFile(prefix, suffix, null)
static File createTempFile(String prefix, String suffix, File directory)
创建临时的文件,文件名的格式为: “prefix” + 随机数 + “suffix”
// 例如: prefix = “temp1”, suffix = “.txt” temp.getAbsolutePath(): E:\Test\temp1753524432639787701.txt
临时文件的创建位置为:directory。如果 directory 为
null
,则在当前系统指定的临时文件夹中创建例如:C:\Users\Tony\AppData\Local\Temp\temp5808249090590052912.txt
创建文件夹
boolean mkdir()和boolean mkdirs()的区别在于:
① mkdirs:创建文件夹,如果有必要,会将父路径中没有的文件夹也一并创建。
② mkdir:只是创建文件夹,当路径中已经有了该文件夹,返回false
。删除
boolean delete()
删除一个文件,文件不存在返回false;文件为文件夹,而且文件夹不为空,返回false
from Java API
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
删除一个对象路径名中指定的文件或者文件夹,如果路径名指定的目标为一个文件夹,文件夹必须为空才能被删除。
void deleteOnExit()
在JVM关闭的时候,删除该文件。该操作会将该文件路径加入到注册器(DeleteOnExitHook类)中,注册器在JVM关闭时执行,删除的顺序按照文件注册的逆序,即新加入的文件最先被删除。
from Java API
Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.
一旦目标文件被注册到注册器中,想反悔是不可能的。所以这个方法应该谨慎使用。// 源码ArrayList<String> toBeDeleted = new ArrayList<>(theFiles);// reverse the list to maintain previous jdk deletion order.// Last in first deleted.Collections.reverse(toBeDeleted);for (String filename : toBeDeleted) { (new File(filename)).delete();}
- 移动
boolean renameTo(File dest)重命名一个文件,如果源文件不存在或者目标文件已经存在则返回false。
获取文件数组
/** Returns an array of strings naming the files and * directories in the directory denoted by this abstract * pathname. */String[] list()/** Returns an array of strings naming the files and * directories in the directory denoted by this abstract * pathname that satisfy the specified filter. */String[] list(FilenameFilter filter)/** Returns an array of abstract pathnames denoting the files * in the directory denoted by this abstract pathname.*/File[] listFiles()/** Returns an array of abstract pathnames denoting the files * and directories in the directory denoted by this abstract * pathname that satisfy the specified filter.*/File[] listFiles(FileFilter filter)/** Returns an array of abstract pathnames denoting the files and * directories in the directory denoted by this abstract pathname * that satisfy the specified filter.*/File[] listFiles(FilenameFilter filter)
这些方法都是用来遍历文件夹用的,遍历中可以指定过滤器。 FileFilter
和 FilenameFilter
是两个文件过滤器接口,支持函数式编程,用于对文件(或者文件名)进行过滤。
boolean java.io.FileFilter.accept(File pathname);boolean java.io.FilenameFilter.accept(File dir, String name);
通向NIO的大门
Path 类是 NIO 对每一个文件元素路径的映射。
Path toPath()
- Java.io.File类的学习
- JAVA\IO\File类学习
- java的java.io.File的学习
- 【Java学习】Java IO-File类的理解和总结
- Java IO学习札记(File类的使用)
- java IO流中File类的学习笔记
- Java学习笔记之IO(一):File类的用法
- Java.io.File类学习笔记
- Java IO之File类学习
- java学习之路---IO-----File类
- j2se学习笔记-java.io.File类
- java IO 学习之 File类 (一)
- Java IO 系统学习之File类
- Java IO 学习之File类
- File类java.io.File
- IO流的学习—File类
- 类 java.io.File
- java.io.File类
- opencv 霍夫圆变换识别网球 通过滑动条来修改参数
- linux缺页异常处理--内核空间
- 框架 day29 Struts2-上传,下载,OGNL与Struts2的结合(值栈与ActionContext),Struts2标签,Token机制
- 2016百度之星资格赛
- Marklogic学习 由浅入深(9)—— 第一个Marklogic应用(中)
- Java.io.File类的学习
- 机器学习之分类算法
- linux缺页异常处理--用户空间
- 调整数组的顺序
- 初始化块
- poj 3335(半平面交)
- 练习26:代码改错
- POJ 1082---->一个神一般的题
- git解决冲突