2008-11-21 9 views
8

जावा में, file.delete() वापसी true या false जहां File file एक गैर-मौजूद फ़ाइल को संदर्भित करता है?क्या file.delete() गैर-मौजूद फ़ाइल के लिए सही या गलत लौटाता है?

मुझे एहसास है कि यह एक बुनियादी सवाल है, और परीक्षण के माध्यम से बहुत आसान है, लेकिन मुझे अजीब परिणाम मिल रहे हैं और पुष्टि की सराहना करेंगे।

उत्तर

4

क्या इसका परिणाम FileNotFoundException में नहीं है?

संपादित करें:

वास्तव में यह झूठी में परिणाम है:

import java.io.File; 

public class FileDoesNotExistTest { 


    public static void main(String[] args) { 
    final boolean result = new File("test").delete(); 
    System.out.println("result: |" + result + "|"); 
    } 
} 

प्रिंट false

1

आधिकारिक जावाडोक:

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. 

Returns: 
    true if and only if the file or directory is successfully deleted; false otherwise 
Throws: 
    SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file 

हां, तो गलत।

8

http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#delete() से:

रिटर्न: सच यदि और केवल यदि फ़ाइल या निर्देशिका है सफलतापूर्वक हटा दिया गया; झूठी अन्यथा

इसलिए, यह एक गैर-मौजूद फ़ाइल के लिए झूठी वापसी करनी चाहिए। निम्नलिखित परीक्षण यह पुष्टि करता है:

import java.io.File; 

public class FileTest 
{ 
    public static void main(String[] args) 
    { 
     File file = new File("non-existent file"); 

     boolean result = file.delete(); 
     System.out.println(result); 
    } 
}

इस कोड को संकलित और चलाने से झूठी पैदा होती है।

संबंधित मुद्दे