यदि कोई फ़ोल्डर मौजूद है, तो कैसे जांचें


199

मैं नए जावा 7 आईओ सुविधाओं के साथ थोड़ा खेल रहा हूं, वास्तव में मैं एक फ़ोल्डर की सभी एक्सएमएल फाइलें प्राप्त करने की कोशिश कर रहा हूं। लेकिन यह एक अपवाद फेंकता है जब फ़ोल्डर मौजूद नहीं है, तो मैं कैसे जांच सकता हूं कि क्या फ़ोल्डर नए IO के साथ मौजूद है?

public UpdateHandler(String release) {
    log.info("searching for configuration files in folder " + release);
    Path releaseFolder = Paths.get(release);
    try(DirectoryStream<Path> stream = Files.newDirectoryStream(releaseFolder, "*.xml")){

        for (Path entry: stream){
            log.info("working on file " + entry.getFileName());
        }
    }
    catch (IOException e){
        log.error("error while retrieving update configuration files " + e.getMessage());
    }


}

2
मुझे आश्चर्य है कि आप यह जांचना चाहेंगे कि क्या फ़ोल्डर मौजूद है। सिर्फ इसलिए कि जब आप चेक करते हैं तो फ़ोल्डर मौजूद होता है, इसका मतलब यह नहीं है कि जब आप फ़ोल्डर बनाते हैं DirectoryStream, तो आप फ़ोल्डर प्रविष्टियों पर पुनरावृति करते हैं।
ओसवाल्ड

जवाबों:


262

का उपयोग कर java.nio.file.Files:

Path path = ...;

if (Files.exists(path)) {
    // ...
}

आप वैकल्पिक रूप से इस विधि LinkOptionमान को पास कर सकते हैं:

if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {

एक विधि भी है notExists:

if (Files.notExists(path)) {

30
इसके अलावा, ध्यान दें कि दोनों Files.exists(path)और Files.notExists(path)एक ही समय में झूठे वापस आ सकते हैं! इसका मतलब यह है कि जावा निर्धारित नहीं कर सकता है कि क्या पथ वास्तव में मौजूद है।
संचित

O_O @Sanchit क्या आपके पास इसे कहने के लिए कुछ मजबूत तर्क हैं?
रिचर्ड

6
प्रलेखन ऐसा कहता है। :) लिंक NotExists की जाँच करें विधि वास्तव में इसे ठीक से लिंक नहीं कर सकती है।
संचित

13
Files.isDirectory (पथ, लिंकऑक्शन);
कानागावेलु सुगुमार

2
@LMaPh !Files.exists(path)और Files.notExists(path)100% समान नहीं हैं। जब जावा निर्धारित नहीं कर सकता है कि क्या कोई फ़ाइल मौजूद है, तो पहला वापस आ जाएगा trueऔर दूसरा वापस आ जाएगा false
जेस्पर

205

काफी सरल:

new File("/Path/To/File/or/Directory").exists();

और यदि आप निश्चित होना चाहते हैं तो यह एक निर्देशिका है:

File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
   ...
}

40
सही उत्तर, लेकिन एक छोटा सा नोटिस: if(f.isDirectory()) {...}यह पर्याप्त होगा, क्योंकि यह जाँच भी अस्तित्व में है।
जी डेमेकी

3
इससे ओपी के सवाल का जवाब नहीं मिलता। java.io.file"नया जावा 7 IO सुविधाओं" का हिस्सा नहीं है जो ओपी का जिक्र कर रहा है। java.nio.fileपैकेज है, जो जावा 7 में पेश किया गया था, प्रदान करता है Pathsऔर Filesकक्षाएं। यहां अन्य उत्तर सही ढंग से बताते हैं कि ओपी समस्या को हल करने के लिए इन नए वर्गों का उपयोग कैसे करें।
डोरोन गोल्ड

53

यह जाँचने के लिए कि क्या कोई निर्देशिका नए IO के साथ मौजूद है:

if (Files.isDirectory(Paths.get("directory"))) {
  ...
}

isDirectoryरिटर्न trueअगर फ़ाइल एक निर्देशिका है; falseयदि फ़ाइल मौजूद नहीं है, तो निर्देशिका नहीं है, या यह निर्धारित नहीं किया जा सकता है कि फ़ाइल निर्देशिका है या नहीं।

देखें: प्रलेखन


6

आपको अपने पथ को Fileअस्तित्व में बदलने और परीक्षण करने की आवश्यकता है:

for(Path entry: stream){
  if(entry.toFile().exists()){
    log.info("working on file " + entry.getFileName());
  }
}

5

अपने फ़ोल्डर निर्देशिका के स्ट्रिंग से एक फ़ाइल उत्पन्न करें

String path="Folder directory";    
File file = new File(path);

और उपयोग विधि मौजूद है।
यदि आप फ़ोल्डर का उपयोग करना चाहते हैं, तो आप mkdir () का उपयोग करें

if (!file.exists()) {
            System.out.print("No Folder");
            file.mkdir();
            System.out.print("Folder created");
        }

4

अलग से exists()विधि को कॉल करने की कोई आवश्यकता नहीं है , क्योंकि isDirectory()अंतर्निहित जांच यह है कि निर्देशिका मौजूद है या नहीं।


4
import java.io.File;
import java.nio.file.Paths;

public class Test
{

  public static void main(String[] args)
  {

    File file = new File("C:\\Temp");
    System.out.println("File Folder Exist" + isFileDirectoryExists(file));
    System.out.println("Directory Exists" + isDirectoryExists("C:\\Temp"));

  }

  public static boolean isFileDirectoryExists(File file)

  {
    if (file.exists())
    {
      return true;
    }
    return false;
  }

  public static boolean isDirectoryExists(String directoryPath)

  {
    if (!Paths.get(directoryPath).toFile().isDirectory())
    {
      return false;
    }
    return true;
  }

}

1
File sourceLoc=new File("/a/b/c/folderName");
boolean isFolderExisted=false;
sourceLoc.exists()==true?sourceLoc.isDirectory()==true?isFolderExisted=true:isFolderExisted=false:isFolderExisted=false;

sourceLoc.isDirectory () रिटर्न बूलियन परिणाम। कोई ज़रूरत नहीं "sourceLoc.isDirectory () == true" का उपयोग करें
ओलेग

1

हम फाइलों की जांच कर सकते हैं और फोल्डर्स को मोड़ सकते हैं।

import java.io.*;
public class fileCheck
{
    public static void main(String arg[])
    {
        File f = new File("C:/AMD");
        if (f.exists() && f.isDirectory()) {
        System.out.println("Exists");
        //if the file is present then it will show the msg  
        }
        else{
        System.out.println("NOT Exists");
        //if the file is Not present then it will show the msg      
        }
    }
}

लगता है कि नेटवर्क साझा फ़ाइल के साथ काम नहीं करता है। पकड़ा गया: org.codehaus.groovy.runtime.typehandling.GroovyCastException: ऑब्जेक्ट 'Z: \\ tierWe bServices \ Deploy \ new.txt' को क्लास 'java.groovy.runtime.GStringImpl' के साथ क्लास 'java.nio' में नहीं डाल सकते। .fi le.Path 'org.codehaus.groovy.runtime.typehandling.GroovyCastException: ऑब्जेक्ट' Z: \\ tierWebService s \ Deploy \ new.txt को क्लास 'org.codehaus.groovy.runtime.GStringImpl' के साथ कक्षा में शामिल नहीं कर सकते। 'java.nio.file.Path'
जिरोंग हू

0

से SonarLint , अगर आप पहले से ही पथ है, का उपयोग path.toFile().exists()करने के बजाय Files.existsबेहतर प्रदर्शन के लिए।

Files.existsविधि JDK 8 में काफ़ी खराब प्रदर्शन है, और जब फ़ाइलें है कि वास्तव में मौजूद नहीं है की जाँच करने के लिए इस्तेमाल काफी एक आवेदन धीमा कर सकते हैं।

उसी के लिए जाता है Files.notExists, Files.isDirectoryऔर Files.isRegularFile

गैर-योग्य कोड उदाहरण:

Path myPath;
if(java.nio.Files.exists(myPath)) {  // Noncompliant
    // do something
}

जटिल समाधान:

Path myPath;
if(myPath.toFile().exists())) {
    // do something
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.