ฉันจะรับไดเร็กทอรีของไฟล์โดยใช้อ็อบเจ็กต์ไฟล์ได้อย่างไร


110

พิจารณารหัส:

File file = new File("c:\\temp\\java\\testfile");

testfileเป็นไฟล์และอาจมีหรือไม่มีอยู่ ฉันต้องการรับไดเรกทอรีc:\\temp\\java\\โดยใช้Fileวัตถุ ฉันจะทำสิ่งนี้ได้อย่างไร?

คำตอบ:


170

ไม่ว่าในกรณีใดฉันคาดหวังfile.getParent()(หรือfile.getParentFile()) ที่จะให้สิ่งที่คุณต้องการ

นอกจากนี้หากคุณต้องการทราบว่าต้นฉบับมีอยู่จริงหรือFile ไม่และเป็นไดเร็กทอรีexists()และisDirectory()เป็นสิ่งที่คุณต้องการ


9
ใช้ file.getParent () อย่างระมัดระวังเนื่องจากอาจส่งคืนค่าว่างในบางกรณี
geschema

คำตอบของ @geschema Ponaguynik ด้านล่างกล่าวถึงสิ่งนี้
4myle


15

หากคุณทำสิ่งนี้:

File file = new File("test.txt");
String parent = file.getParent();

parent จะเป็นโมฆะ

เพื่อรับไดเร็กทอรีของไฟล์นี้คุณสามารถทำได้ต่อไป:

parent = file.getAbsoluteFile().getParent();

8

File API File.getParentหรือFile.getParentFileควรส่งคืน Directory of file ให้คุณ

รหัสของคุณควรเป็นดังนี้:

    File file = new File("c:\\temp\\java\\testfile");
    if(!file.exists()){
        file = file.getParentFile();
    }

คุณสามารถตรวจสอบไฟล์พาเรนต์ของคุณเพิ่มเติมได้โดยใช้File.isDirectory API

if(file.isDirectory()){
    System.out.println("file is directory ");
}

4
File directory = new File("Enter any 
                directory name or file name");
boolean isDirectory = directory.isDirectory();
if (isDirectory) {
  // It returns true if directory is a directory.
  System.out.println("the name you have entered 
         is a directory  : "  +    directory);  
  //It returns the absolutepath of a directory.
  System.out.println("the path is "  + 
              directory.getAbsolutePath());
} else {
  // It returns false if directory is a file.
  System.out.println("the name you have
   entered is a file  : " +   directory);
  //It returns the absolute path of a file.
  System.out.println("the path is "  +  
            file.getParent());
}

1
คุณไม่ตอบคำถามนี้ใช้ไม่ได้กับไฟล์
toni07

codeไฟล์สุดท้าย = ไฟล์ใหม่ ("C: /dev/changeofseasons.mid"); System.out.println ("มีไฟล์อยู่หรือไม่" + file.exists ()); System.out.println ("ไดเร็กทอรีของไฟล์:" + file.getAbsolutePath ()); โอเคขอโทษสำหรับการเยื้องที่ง่อยฉันไม่คิดว่าจะจัดรูปแบบโค้ดในความคิดเห็นได้ ยังคงเห็นได้ชัดว่ารหัสของคุณใช้งานไม่ได้
toni07

ขอบคุณแก้ไข <! --->
Jigar Joshi

3
File filePath=new File("your_file_path");
String dir="";
if (filePath.isDirectory())
{
    dir=filePath.getAbsolutePath();
}
else
{
    dir=filePath.getAbsolutePath().replaceAll(filePath.getName(), "");
}

จำเป็นต้องมีคำอธิบาย
Halvor Holsten Strand

1
ยินดีต้อนรับสู่ Stack Overflow! โดยทั่วไปคำตอบของรหัสต้องการคำอธิบายเล็กน้อย - ดูโพสต์ meta Stackoverflowนี้ ด้วยคำตอบที่คุณโพสต์ไว้คุณอาจต้องอธิบายว่าคุณกำลังพยายามให้กรณีทั่วไปและเกี่ยวข้องกับโพสต์จริงของ OP อย่างไร อย่างจริงจังมากขึ้น - คุณอาจต้องการพิจารณาว่ามันจะทำงานyour_file_path = "C:\\testfiles\\temp\\testfile";อย่างไร - ฉันไม่คิดว่ามันจะให้สิ่งที่คุณหวัง
J Richard Snape

ควรเป็นคำตอบที่ถูกต้อง. ซึ่งจะแสดงเส้นทางที่สมบูรณ์ไปยังไฟล์
Magno C



-1

ฉันพบว่าสิ่งนี้มีประโยชน์มากขึ้นสำหรับการรับตำแหน่งไฟล์ที่แน่นอน

File file = new File("\\TestHello\\test.txt");
System.out.println(file.getAbsoluteFile());
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.