ทำไมเราต้องตรวจสอบว่าไฟล์ "มีอยู่" หรือไม่
ในขณะที่จัดการกับการทำงานของไฟล์ (อ่าน/เขียน/สร้าง/ลบ/อัปเดต ฯลฯ) มือใหม่หลายคนอาจสงสัยว่าทำไมเราต้องตรวจสอบว่ามีไฟล์อยู่หรือไม่ การตอบสนองที่เหมาะสมสำหรับสิ่งนี้คือ เพื่อหลีกเลี่ยงNoSuchFileExceptionจึงเป็นวิธีที่ปลอดภัยกว่าเสมอในการเข้าถึงไฟล์ ดังนั้น คุณต้องตรวจสอบว่ามีไฟล์อยู่หรือไม่ก่อนที่จะเข้าถึงเพื่อหลีกเลี่ยงข้อยกเว้นรันไทม์
จะตรวจสอบโดยใช้เมธอด file.exists() ได้อย่างไร?
Java มีเมธอดบูลีนอย่างง่ายfile.exists()ที่ไม่ต้องการพารามิเตอร์ใดๆ เพื่อตรวจสอบไฟล์ที่เกี่ยวข้องบนพาธที่กำหนด เมื่อตรวจสอบการมีอยู่ของไฟล์ ให้พิจารณา 3 สถานการณ์- พบไฟล์.
- ไม่พบไฟล์
- สถานะไฟล์ไม่เป็นที่รู้จักหากไม่ได้รับอนุญาต (เนื่องจากเหตุผลด้านความปลอดภัย)
ตัวอย่าง
มาดูตัวอย่างโค้ดง่ายๆ เพื่อดูการใช้งานกัน
package com.java.exists;
import java.io.File;
public class ExistsMethodInJava {
public static void main(String[] args) {
String filePath = "C:\\Users\\Lubaina\\Documents\\myNewTestFile.txt";
File file = new File(filePath);
// check if the file exists at the file path
System.out.println("Does File exists at \"" + filePath + "\"?\t" + file.exists());
filePath = "C:\\Users\\Lubaina\\Documents\\myOtherTestFile.txt";
File nextFile = new File(filePath);
// check if the file exists at the file path
System.out.println("Does File exists at \"" + filePath + "\"?\t" + nextFile.exists());
}
}
เอาต์พุต
มีไฟล์อยู่ที่ "C:\Users\Lubaina\Documents\myNewTestFile.txt" หรือไม่ ไฟล์มีอยู่จริงที่ "C:\Users\Lubaina\Documents\myOtherTestFile.txt" หรือไม่ เท็จ
โปรดทราบว่า เมธอด file.exists()ใช้ได้กับพาธ “ ไดเร็กทอรี ” ด้วย หากคุณตรวจสอบเส้นทางไดเร็กทอรีที่ถูกต้องด้วยวิธีนี้ จะส่งกลับค่าจริงหรือเท็จหากไม่เป็นเช่นนั้น เพื่อความเข้าใจที่ดีขึ้น คุณสามารถดูบล็อกโค้ดต่อไปนี้
package com.java.exists;
import java.io.File;
public class CheckFileExists {
// check if the "file" resource exists and not "directory"
public static boolean checkFileExists(File file) {
return file.exists() && !file.isDirectory();
}
public static void main(String[] args) {
String directoryPath = "C:\\Users\\Lubaina\\Documents\\javaContent";
File direcotry = new File(directoryPath);
// check if the directory exists at the dir path
if (direcotry.exists()) {
System.out.println("Direcotry at \"" + directoryPath + "\" exists.\n");
} else {
System.out.println("Direcotry at \"" + directoryPath + "\" does not exist.\n");
}
// check if the resource present at the path is a "file" not "directory"
boolean check = checkFileExists(direcotry);
System.out.println("Is the resource \"" + direcotry + "\" a File? " + check);
String filePath = "C:\\Users\\Lubaina\\Documents\\myNewTestFile.txt";
File file = new File(filePath);
check = checkFileExists(file);
System.out.println("Is the resource \"" + file + "\" a File? " + check);
}
}
เอาต์พุต
มีไดเร็กทอรีที่ "C:\Users\Lubaina\Documents\javaContent" ทรัพยากร "C:\Users\Lubaina\Documents\javaContent" เป็นไฟล์หรือไม่ เท็จ ทรัพยากร "C:\Users\Lubaina\Documents\myNewTestFile.txt" เป็นไฟล์หรือไม่ จริง
ดังที่คุณเห็นจากผลลัพธ์ ไดเร็กทอรีชื่อ “javaContent” ได้รับการตรวจสอบความถูกต้องโดยวิธีการที่มีอยู่ () ดังนั้นหากคุณต้องการตรวจสอบว่าไฟล์ไม่ใช่ไดเร็กทอรีโดยเฉพาะ คุณสามารถใช้เมธอดบูลีนisDirectory()ที่จัดเตรียมโดย คลาส Fileใน Java
บทสรุป
ในตอนท้ายของบทความนี้ คุณต้องคุ้นเคยกับวิธีการตรวจสอบว่ามีไฟล์อยู่ใน Java หรือไม่ คุณสามารถเขียนโปรแกรมของคุณเองเพื่อทดสอบและทำความเข้าใจกับฟังก์ชันนี้ เมื่อคุณคุ้นเคยกับมันแล้ว คุณสามารถสำรวจวิธีอื่นๆ ในการตรวจสอบการมีอยู่ของไฟล์ (เช่น การใช้ลิงก์สัญลักษณ์หรือคลาส nio) ได้เช่นกัน ขอให้โชคดีและมีความสุขในการเขียนโค้ด! :)
อ่านเพิ่มเติม: |
---|
GO TO FULL VERSION