ไฟล์ 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 Packages Tutorialของเรา
ชั้น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 |
คุณจะได้เรียนรู้วิธีสร้าง เขียน อ่าน และลบไฟล์ในบทต่อไป: