लोकल फ़ाइलें ढूँढना
आप एक छोटा प्रोग्राम लिखना चाहते हैं जो दिए गए डायरेक्टरी में सिर्फ़ लोकल फ़ाइलें प्रिंट करे। आपने डायरेक्टरी के एलिमेंट्स की लिस्ट निकाल ली है, जो DIRECTORY_CONTENT में स्टोर है, लेकिन:
- हर एलिमेंट में पहले type होता है, फिर एलिमेंट
/dका मतलब directory है,/fका मतलब file है- कुछ फ़ाइलें hidden होती हैं, जिनमें
"."आता है - दूसरी directories में मौजूद फ़ाइलें प्रिंट नहीं होनी चाहिए आइए, अब इन्हें ठीक करते हैं!
यह अभ्यास पाठ्यक्रम का हिस्सा है
इंटरमीडिएट Java
अभ्यास निर्देश
- एलिमेंट file है या directory, यह जाँचने के लिए
.substring(a,b)और.contains("")का इस्तेमाल करें. - जब किसी फ़ाइल में
"."containsहो, तब जाँचें कि वह hidden है. - यह जाँचें कि फ़ाइल local है, यानी उसमें
"/"नहीं होना चाहिए. - दोनों शर्तों को एक साथ जाँचने के लिए सही logical operator का उपयोग करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
class ls {
static boolean isFile(String elem) {
// Check that the first 3 characters of the element contain /f as a substring
return elem.____(0, 3).____("/f");
}
static boolean isHidden(String elem) {
// Use the appropriate method to make sure that file is hidden
return elem.____(".");
}
static boolean isNonLocal(String elem) {
// Use the correct method to determine whether a file is in a directory
return elem.____(____, elem.length()).____(____);
}
public static void main(String[] args) {
int hiddenCounter = 0, directoryCounter = 0, nestedCounter = 0;
for (String elem : DIRECTORY_CONTENT) {
if (isFile(elem)) {
if (!isHidden(elem)) System.out.print(elem.substring(2));
// Use a logical operator to make it correct
else if (isHidden(elem) ____ !isNonLocal(elem)) hiddenCounter++;
else nestedCounter++;
} else directoryCounter++;
}
printer(hiddenCounter, directoryCounter, nestedCounter);
}
static String[] DIRECTORY_CONTENT = {"/d .Trash", "/f .history", "/d Applications", "/f tmp", "/f script", "/d Documents", "/f Documents/.bankAccounts", "/f .sshKeys", "/d Pictures", "/f content", "/f Documents/file"};
static void printer(int hiddenCounter, int directoryCounter, int nestedCounter) {
System.out.println();
System.out.println("With :\n" + hiddenCounter + " hidden files,\n" + directoryCounter + " directories,\nAnd " + nestedCounter + " nested files");
}
}