目录

JAVA 文件


文件处理是任何应用程序的重要组成部分。

Java 有多种创建、读取、更新和删除文件的方法。


Java 文件处理

这个File类来自java.io包,允许我们处理文件。

要使用Fileclass,创建该类的对象,并指定文件名或目录名:

示例

import java.io.File;  // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

如果您不知道什么是包,请阅读我们的Java 包教程

这个File类有许多有用的方法来创建和获取有关文件的信息。例如:

Method Type Description
canRead() Boolean Tests whether the file is readable or not
canWrite() Boolean Tests whether the file is writable or not
createNewFile() Boolean Creates an empty file
delete() Boolean Deletes a file
exists() Boolean Tests whether the file exists
getName() String Returns the name of the file
getAbsolutePath() String Returns the absolute pathname of the file
length() Long Returns the size of the file in bytes
list() String[] Returns an array of the files in the directory
mkdir() Boolean Creates a directory

您将在接下来的章节中学习如何创建、写入、读取和删除文件: