最近接触到了一些关于文件生成以及文件内容写入的工作任务,关于文件的一些方法常用的是 java.io.File包中的一些方法。这些方法是在java1.0的时候提供的。然后在File类的注释中看到了这个包java.nio.file.Files;随后查看了一下这个包。简单了解了一下
File
/**
* An abstract representation of file and directory pathnames.
.......
* <h3>Interoperability with {@code java.nio.file} package</h3>
*
* <p> The <a href="../../java/nio/file/package-summary.html">{@code java.nio.file}</a>
* package defines interfaces and classes for the Java virtual machine to access
* files, file attributes, and file systems. This API may be used to overcome
* many of the limitations of the {@code java.io.File} class.
* The {@link #toPath toPath} method may be used to obtain a {@link
* Path} that uses the abstract path represented by a {@code File} object to
* locate a file. The resulting {@code Path} may be used with the {@link
* java.nio.file.Files} class to provide more efficient and extensive access to
* additional file operations, file attributes, and I/O exceptions to help
* diagnose errors when an operation on a file fails.
*
* @author unascribed
* @since JDK1.0
*/
public class File
implements Serializable, Comparable<File>
{
.....
}
从这段注释的说明中可以看出Files类是对原File类的一些操作的扩展。
Files
/**
* This class consists exclusively of static methods that operate on files,
* directories, or other types of files.
*
* <p> In most cases, the methods defined here will delegate to the associated
* file system provider to perform the file operations.
*
* @since 1.7
*/
public final class Files {
private Files() {
}
....
}
这个包是nio下的关于文件操作的一系列类似于工具包的类。这个类中只包含了对文件,目录和其他类型的文件操作的静态方法。
查了 一下关于这个类的使用是与java.nio.file.Path一起使用的。
关于Path类是可以使用在多线程的情况下。
/**
* An object that may be used to locate a file in a file system. It will
* typically represent a system dependent file path.
.....
* <h2>Concurrency</h2>
* <p> Implementations of this interface are immutable and safe for use by
* multiple concurrent threads.
*
* @since 1.7
* @see Paths
*/
public interface Path
extends Comparable<Path>, Iterable<Path>, Watchable
{
/**
* Returns the file system that created this object.
*
* @return the file system that created this ob