File Java
Penanganan file adalah bagian penting dari aplikasi apa pun.
Java memiliki beberapa metode untuk membuat, membaca, memperbarui, dan menghapus file.
Penanganan File Java
Kelas File
dari java.io
paket, memungkinkan kita untuk bekerja dengan file.
Untuk menggunakan File
kelas, buat objek kelas, dan tentukan nama file atau nama direktori:
Contoh
import java.io.File; // Import the File class
File myObj = new File("filename.txt"); // Specify the filename
Jika Anda tidak tahu apa itu paket, baca Tutorial Paket Java kami .
Kelas File
memiliki banyak metode yang berguna untuk membuat dan mendapatkan informasi tentang file. Sebagai contoh:
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 |
Anda akan mempelajari cara membuat, menulis, membaca, dan menghapus file di bab berikutnya: