2012-01-19 13 views
19

बराबर है, मुझे आपके बारे में पता नहीं है लेकिन कम से कम मुझे उम्मीद है कि एफ 1 नीचे दिए गए कोड में f2 के बराबर होगा लेकिन स्पष्ट रूप से यह मामला नहीं है! इसके बारे में आपके विचार क्या हैं? ऐसा लगता है कि मुझे इसका समर्थन करने के लिए अपनी खुद की बराबर विधि लिखनी है, है ना?जावा फ़ाइल

import java.io.*; 

public class FileEquals 
{ 
    public static void main(String[] args) 
    { 
     File f1 = new File("./hello.txt"); 
     File f2 = new File("hello.txt"); 
     System.out.println("f1: " + f1.getName()); 
     System.out.println("f2: " + f2.getName()); 
     System.out.println("f1.equals(f2) returns " + f1.equals(f2)); 
     System.out.println("f1.compareTo(f2) returns " + f1.compareTo(f2)); 
    } 
} 
+1

जावा 7 की पथ कक्षा के साथ ऐसा ही होता है। लेकिन Path.normalize() या Files.isSameFile() – Luciano

+0

जैसी विधियां मौजूद हैं, आप वास्तविक आउटपुट दिखाकर कुछ समय इस प्रश्न के सभी दर्शक सुरक्षित कर सकते हैं। मैं उम्मीद कर रहा था कि 'बराबर' और 'तुलना करने के लिए' परिणामों के विपरीत थे। यह मामला नहीं है, 'बराबर' रिटर्न झूठा है और 'तुलना करने के लिए' रिटर्न -58, जिसका अर्थ है "लेक्सिकोग्राफिक" से कम "। @ लुइसियानो: ध्यान दें कि 'Files.isSameFile' इस मामले में फ़ाइलों को खोलने का प्रयास करेगा क्योंकि पथ बराबर नहीं हैं और 'NoSuchFileException' के साथ विफल हो सकते हैं। – bluenote10

उत्तर

30

नहीं, यह मामला नहीं है। क्योंकि के बराबर होती है यह ऊपर अपने मामले में पूर्ण पथ की समानता (तुलना कर रहा है कुछ की तरह है:।

some-project\.\hello.txt 
some-project\hello.txt 

तो वे स्वाभाविक रूप से अलग हैं

ऐसा लगता है जैसे मैं लिखने की मेरी अपनी विधि के बराबर होती है , यह समर्थन करने के लिए है ना?

शायद हाँ। लेकिन सब से पहले, आप को पता है कि आप तुलना करना चाहते है? केवल pathnames? यदि हाँ, अपने विहित पथ की तुलना इस तरह से:

f1.getCanonicalPath().equals(f2.getCanonicalPath()) 

लेकिन अगर आप दो अलग अलग फ़ाइलों, तो हाँ की सामग्री की तुलना करना चाहते हैं, आप अपने खुद के विधि लिखना चाहिए - या बस सिर्फ इंटरनेट पर कहीं से नकल।

सादर

+1

मैं वास्तव में कुछ "फाइललिस्ट" करना चाहता हूं।इसमें (फ़ाइल) "है और यह विधि बराबर विधि को कॉल करती है। – aandeers

1

यहाँ दोनों तरीकों के कार्यान्वयन है:

/** 
* Tests this abstract pathname for equality with the given object. 
* Returns <code>true</code> if and only if the argument is not 
* <code>null</code> and is an abstract pathname that denotes the same file 
* or directory as this abstract pathname. Whether or not two abstract 
* pathnames are equal depends upon the underlying system. On UNIX 
* systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows 
* systems it is not. 
* 
* @param obj The object to be compared with this abstract pathname 
* 
* @return <code>true</code> if and only if the objects are the same; 
*   <code>false</code> otherwise 
*/ 
public boolean equals(Object obj) { 
    if ((obj != null) && (obj instanceof File)) { 
     return compareTo((File)obj) == 0; 
    } 
    return false; 
} 
/** 
* Compares two abstract pathnames lexicographically. The ordering 
* defined by this method depends upon the underlying system. On UNIX 
* systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows 
* systems it is not. 
* 
* @param pathname The abstract pathname to be compared to this abstract 
*     pathname 
* 
* @return Zero if the argument is equal to this abstract pathname, a 
*   value less than zero if this abstract pathname is 
*   lexicographically less than the argument, or a value greater 
*   than zero if this abstract pathname is lexicographically 
*   greater than the argument 
* 
* @since 1.2 
*/ 
public int compareTo(File pathname) { 
    return fs.compare(this, pathname); 
} 
0

आप विंडोज उपयोग कर रहे हैं देखने के वर्ग Win32FileSystem

तुलना विधि नीचे की तरह है, इसलिए यह बहुत है सामान्य है कि आपकी फ़ाइल ऑब्जेक्ट्स अलग हैं।

public int compare(File f1, File f2) { 
     return f1.getPath().compareToIgnoreCase(f2.getPath()); 
    } 

रूप में अच्छी तरह

 System.out.println(f1.getPath()); 
     System.out.println(f2.getPath()); 

अपने कोड में उन पंक्तियों के जोड़े और यह तुलना फ़ाइल वस्तु

की राह proeprty का उपयोग किया जाता है वे बराबर नहीं हैं

.\hello.txt 
hello.txt 

प्रिंट होगा इसलिए

4

यदि आप केवल प्रत्येक फ़ाइल की सामग्री की तुलना करना चाहते हैं, तो आप सामग्री को बाइट एआर में पढ़ सकते हैं इस तरह की किरण:

byte[] f1 = Files.readAllBytes(file1); 
byte[] f2 = Files.readAllBytes(file2); 

और फिर वही तुलना करें जो आप वहां से चाहते हैं।

ध्यान दें कि यह विधि कॉल केवल जावा 7 में मौजूद है। पुराने संस्करणों के लिए, गुवा और अपाचे के पास समान नाम और विधियों के साथ समान तरीके हैं।

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

FileInputStream f1 = new FileInputStream(file1); 
DataInputStream d1 = new DataInputStream(f1); 
FileInputStream f2 = new FileInputStream(file2); 
DataInputStream d2 = new DataInputStream(f2); 

byte b1 = d1.readByte(); 
byte b2 = d2.readByte(); 

और फिर तुलना: या एक बेहतर विकल्प (खासकर यदि आप बड़ी फ़ाइलों की तुलना कर रहे) बस, बल्कि स्मृति में पूरे फ़ाइल लोड करने से बाइट द्वारा बाइट तुलना इस तरह हो सकता है वहां से।

+0

+1 अच्छी पोस्ट - मैंने आज कुछ सीखा है (मैंने अभी तक जावा 7 का उपयोग नहीं किया है, यह देखने में खुशी है कि उन्होंने एक फाइल यूटिलिटी जोड़ा है) – user949300

+1

मैं उपलब्ध होने पर पहले फाइलों के आकार की तुलना करूंगा। – Luciano

+5

यह – unbeli

7

ठीक से परीक्षण के बराबर होती है के लिए, आपको getCanonicalFile() कॉल करना होगा। जैसे

public static void main(String[] args) throws IOException 
    { 
     File f1 = new File("./hello.txt").getCanonicalFile(); 
     File f2 = new File("hello.txt").getCanonicalFile(); 
     System.out.println("f1: " + f1.getAbsolutePath()); 
     System.out.println("f2: " + f2.getAbsolutePath()); 
     System.out.println("f1.equals(f2) returns " + f1.equals(f2)); 
     System.out.println("f1.compareTo(f2) returns " + f1.compareTo(f2)); 
    } 

बराबर के लिए सच वापस आ जाएगा। ध्यान दें कि GetCanonicalFile एक IOException फेंक सकता है इसलिए मैंने विधि हस्ताक्षर में जोड़ा।

2

दो फाइलों पर भिन्न होने के लिए मुझे जितना तेज़ तरीका मिला है, वह नीचे है।

यह सिर्फ इसके आसपास काम करने का प्रस्ताव है।

प्रदर्शन के बारे में सुनिश्चित नहीं हैं कि (क्या हुआ अगर फ़ाइलों 10 जीबी प्रत्येक? हैं)

File file = new File("/tmp/file.txt"); 
    File secondFile = new File("/tmp/secondFile.txt"); 

    // Bytes diff 
    byte[] b1 = Files.readAllBytes(file.toPath()); 
    byte[] b2 = Files.readAllBytes(secondFile.toPath()); 

    boolean equals = Arrays.equals(b1, b2); 

    System.out.println("the same? " + equals); 

    // List Diff 
    List<String> c1 = Files.readAllLines(file.toPath()); 
    List<String> c2 = Files.readAllLines(secondFile.toPath()); 

    boolean containsAll = c1.containsAll(c2); 
    System.out.println("the same? " + containsAll);     
} 

संपादित

लेकिन फिर भी, यूनिक्स सिस्टम पर diff उपयोगिता बहुत जल्दी और वर्बोज़ होगा। निर्भर करता है कि आपको तुलना करने की आवश्यकता है।

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