Javaファイル
ファイル処理は、あらゆるアプリケーションの重要な部分です。
Javaには、ファイルを作成、読み取り、更新、および削除するためのいくつかの方法があります。
Javaファイル処理
パッケージのFile
クラスをjava.io
使用すると、ファイルを操作できます。
クラスを使用するには、File
クラスのオブジェクトを作成し、ファイル名またはディレクトリ名を指定します。
例
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 |
次の章では、ファイルの作成、書き込み、読み取り、および削除の方法を学習します。