मैं वर्तमान में कुछ पथ ट्रैवर्सल से संबंधित सुरक्षा तंत्र में जांच कर रहा हूं और java.io.File.getCanonicalPath() के अजीब व्यवहार में आया हूं। मैंने सोचा कि CanonicalPath हमेशा सार अंतर्निहित फ़ाइल के सही अद्वितीय पथ का प्रतिनिधित्व करेगा। हालांकि अगर फ़ाइल नाम में दो बिंदुओं के बाद एक स्थान होता है तो कैननिकलपैथ अब सही पथ का प्रतिनिधित्व नहीं कर रहा है।जावा फ़ाइल कैनोनिकलपैथ पूंछ '..' के साथ असंगत व्यवहार की ओर जाता है
यहाँ उदाहरण है:
File root = new File("c:/git/");
String relative = ".. /.. \\";
File concatFile = new File (root.getCanonicalPath(), relative);
System.out.println("ConcatFileAbsolute: '" + concatFile.getAbsolutePath() + "'");
System.out.println("ConcatFileCanonical: '" + concatFile.getCanonicalPath() + "'");
File canonFile = new File(concatFile.getCanonicalPath());
System.out.println("\ncanonFileCanonical: '" + canonFile.getCanonicalPath() + "'");
System.out.println("canonFileAbsolute: '" + canonFile.getAbsolutePath() + "'");
System.out.println("canonFileName: '" + canonFile.getName() + "'\n");
for (File file : canonFile.listFiles()) {
System.out.println("canon: '" + file.getCanonicalPath() + "' - absolute: '" + file.getAbsolutePath()+ "'");
}
कंसोल आउटपुट:
ConcatFileAbsolute: 'C:\git\.. \.. '
ConcatFileCanonical: 'C:\git\.. \'
canonFileCanonical: 'C:\git\'
canonFileAbsolute: 'C:\git\.. '
canonFileName: '.. '
canon: 'C:\git\.. \$Recycle.Bin' - absolute: 'C:\git\.. \$Recycle.Bin'
canon: 'C:\git\.. \.m2' - absolute: 'C:\git\.. \.m2'
canon: 'C:\git\.. \boot' - absolute: 'C:\git\.. \boot'
...other content of C:/
आप देख सकते हैं, हालांकि canonFile की canonicalPath स्पष्ट रूप से इंगित यह सी में स्थान दिया गया है: \ Git \, लेकिन .listFiles सूचियों सी में सभी फाइलें:।
यह केवल तब होता है जब मैं नई फ़ाइल (स्ट्रिंग, स्ट्रिंग) कन्स्ट्रक्टर का उपयोग करता हूं। अगर मैं फ़ाइल को concatFile = नई फ़ाइल (root.getCanonicalPath() + रिश्तेदार) में तीसरी पंक्ति बदलता हूं; तो आउटपुट के रूप में की उम्मीद है:
ConcatFileAbsolute: 'C:\git.. \.. '
ConcatFileCanonical: 'C:\git\'
- The following output then lists files under C:\git\
यदि इस व्यवहार यूनिक्स सिस्टम पर इसी तरह की है मैं नहीं जानता।
क्या कोई इस व्यवहार को स्पष्ट कर सकता है? क्या इसका इरादा है?
धन्यवाद अग्रिम!
आपको अपने प्रश्न का कुछ जोड़ने के लिए, प्रश्न को संपादित चाहते हैं, कोड:
यह बिल्कुल वैसा ही परिणाम देता है टिप्पणियों में काफी हद तक अपठनीय है। – pvg