File类
JDK1.0引入的File类,在Java语言的java.io包中,由File类提供了描述文件和目录的操作与管理方法。但File类不是InputStream、OutputStream或Reader、Writer的子类,因为它不负责数据的输入输出,而专门用来
管理磁盘文件与目录
注意:File 类只能操作文件的属性,文件的内容是不能操作的。
然鹅,在Java7中新加的Path和Files类,用起来比File类要方便的多,File类网上的教程也比较多,所以,在此就不过多介绍File类了,直接上更硬的菜。
Path
和
Files
Path类
一、Path类的构建
path类的创建主要是借助Paths类。
1. Paths.get()
static Path get(String first, String... more);
静态的Paths.get()方法接受一个或多个字符串,并将它们用默认文件系统的路径分隔符(类UNIX文件系统是/,Windows是\ )连接起来,然后解析他们的结果。
如果其表示的不是给定文件系统的合法路径,就会抛出InvalidPathException
。
使用案例(类UNIX环境下):
Path absolute = Paths.get("/home", "harry");
Path relative = Paths.get("myprog", "conf", "user.properties");
String baseDir = props.getProperty("base.dir");
Path basePath = Paths.get(baseDir);
2. path1.resolve(path2)
Path resolve(Path other);
Path resolve(String other);
用来组合或解析路径
如果path2是绝对路径,则结果就是path2
否值,根据文件系统的柜子,将“path1后面跟着path2”作为结果
3. path1.resolveSibling(path2)
Path resolveSibling(Path other);
Path resolveSibling(String other);
这个方法通过解析指定路径的父路径产生其兄弟路径。
举个例子:若workPath 是 /opt/myapp/work,那么下面的调用
Path tempPath = workPath.resolveSibling("temp");
将创建一个路径为/opt/myapp/temp的Path赋给tempPath。
二、Path类的其他API
Path toAbsolutePath()
Path getParent()
Path getFileName()
Path getRoot()
toFile()
Files类
一、通过Files简化读写文件
static byte[] readAllBytes(Path path)
static List<String> readAllLines(Path path, Charset charset)
static Path write(Path path, byte[] contents, OpenOption...options)
static Path write(Path path, Iterable<? extends CharSequence> contents, OpenOption options)
static InputStream newInputStream(Path path, OpenOption... options)
static OutputStream newoutputStream(Path path, OpenOption... options)
static BufferedReader newBufferedReader(Path path, Charset charset)
static BufferedWriter newBufferedWriter(Path path, Charset charset, OpenOption...options)
这些便捷方法可以将你从处理FileInputStream、FileOutputStream、BufferedReader和BufferedWriter的繁复操作中解脱出来。
read,wirte这一系列简便的方法适用于处理中等长度的文本文件,如果要处理的文件长度比较大,或者是二进制文件,那么还是应该使用所熟知的流或者读入器/写出器:
InputStream in = Files.newInputStream(path);
OutputStream out = Files.newOutputStream ( path );
Reader in = Files.newBufferedReader(path, charset);
Writer out = Files.newBufferedWriter(path, charset );
二、通过Files创建文件和目录
static Path createFile(Path path, FileAttribute<?>... attrs)
static Path createDirectory(Path path, FileAttribute<?>... attrs)
static Path createDirectories ( Path path , FileAttribute<?>... attrs )
static Path createTempFile(String prefix, string suffix, FileAttribute<?>... attrs)
static Path createTempFile(Path parentDir, String prefix, String suffix, FileAttribute<?>... attrs)
static Path createTempDirectory ( String prefix, FileAttribute<?>... attrs)
static Path createTempDirectory(Path parentDir, String prefix, FileAttribute<?>... attrs)
三、通过Files复制、移动和删除文件
static Path copy(Path from, Path to, CopyOption... options)
static Path move(Path from, Path to, CopyOption... options)
static long copy(Inputstream from, Path to, CopyOption... options)
static long copy(Path from, OutputStream to, CopyOption... options)
static void delete(Path path)
static boolean deleteIfExists(Path path)
用于文件操作的标准选项表如下:
四、通过Filse获取文件信息
static boolean exists(Path path)
static boolean isHidden(Path path)
static boolean isReadable(Path path)
static boolean iswritable(Path path)
static boolean isExecutable(Path path)
static boolean isRegularFile(Path path)
static boolean isDirectory(Path path)
static boolean isSymbolicLink(Path path)
static long size(Path path)
A readAttributes ( Path path , Class<A> type , LinkOption ... options )
通过Files.readAttributes(path, BasicFileAttributes.class);
获得相关路径的BasicFileAttributes对象,从而调用下面的api
FileTime creationTime()
FileTime lastAccessTime()
FileTime lastModifiedTime()
boolean isRegularFile()
boolean isDirectory()
boolean isSymbolicLink()
long size()
Object filekey()
五、其他Files的操作
Files还有其他一些操作,如:
访问目录中的项
使用目录流
因为这些操作涉及Stream,而目前我还没去了解Stream的知识,所以就不在这里班门弄斧啦,感兴趣的朋友可以去官网查看API文档。
RandomAccessFile类
一、RandomAccessFile简介
RandomAccessFile既可以读取文件内容,也可以向文件输出数据。同时,RandomAccessFile支持“随机访问”的方式,程序快可以直接跳转到文件的任意地方来读写数据。
由于RandomAccessFile可以自由访问文件的任意位置,所以如果需要访问文件的部分内容,而不是把文件从头读到尾,使用RandomAccessFile将是更好的选择。
与OutputStream、Writer等输出流不同的是,RandomAccessFile允许自由定义文件记录指针,RandomAccessFile可以不从开始的地方开始输出,因此RandomAccessFile可以向已存在的文件后追加内容。如果程序需要向已存在的文件后追加内容,则应该使用RandomAccessFile。
RandomAccessFile的方法虽然多,但它有一个最大的局限,就是只能读写文件,不能读写其他IO节点。
二、RandomAccessFile的方法
RandomAccessFile(String file, String mode)
RandomAccessFile(File file, String mode)
long getFilePointer()
void seek ( long pos )
long length()
1. RandomAccessFile构造函数
RandomAccessFile类有两个构造函数,其实这两个构造函数基本相同,只不过是指定文件的形式不同——一个需要使用String参数来指定文件名,一个使用File参数来指定文件本身。除此之外,创建RandomAccessFile对象时还需要指定一个mode参数,该参数指定RandomAccessFile的访问模式,一共有4种模式。
**"r" : ** 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
"rw": 打开以便读取和写入。
"rws": 打开以便读取和写入。相对于 "rw","rws" 还要求对“文件的内容”或“元数据”的每个更新都同步写入到基础存储设备。
"rwd" : 打开以便读取和写入,相对于 "rw","rwd" 还要求对“文件的内容”的每个更新都同步写入到基础存储设备。
2. RandomAccessFile重要方法
RandomAccessFile既可以读文件,也可以写文件,所以类似于InputStream的read()方法,以及类似于OutputStream的write()方法,RandomAccessFile都具备。除此之外,RandomAccessFile具备两个特有的方法,来支持其随机访问的特性。
RandomAccessFile对象包含了一个记录指针,用以标识当前读写处的位置,当程序新创建一个RandomAccessFile对象时,该对象的文件指针记录位于文件头(也就是0处),当读/写了n个字节后,文件记录指针将会后移n个字节。除此之外,RandomAccessFile还可以自由移动该记录指针。下面就是RandomAccessFile具有的两个特殊方法,来操作记录指针,实现随机访问:
long getFilePointer( ):返回文件记录指针的当前位置
void seek(long pos ):将文件指针定位到pos位置