in Education by
Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can one test, if the later file path would represent a real file on disk, which is below or equal the directory represented by the first string. Here are some more examples to clarify this point, including some sample return values of a checking function that is questioned here: /path/to/dir /path/to/dir (true) /path/to/dir /path/to/dir/file (true) /path/to/dir /path (false) /path/to/dir /path/to/dir/../../file (false) /path/to/dir file (false) /path/to/dir /path/to/dir/dir2/../ (true) Would one get rid of the dots by the File method .getCanonicalPath and then check on a string level? Maybe there is a better way. I could not find out, what File's .compareTo does exactly. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Please use native path operations. String operations may introduce errors due to incorrect handling of path separators, etc. Path path1 = Paths.get("/home").normalize(); Path path2 = Paths.get("/home/user/filename").normalize(); path2.startsWith(path1);

Related questions

0 votes
    Consider two given file paths as strings: /path/to/dir and /path/to/dir/file In Java, how can ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    When two threads access the same ArrayList object what is the outcome of the program? (a) Both are able ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What happens if two threads simultaneously modify TreeSet? (a) ConcurrentModificationException is thrown (b) Both threads ... of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What will happen if two thread of the same priority are called to be processed simultaneously? (a) Anyone ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I am able to modify my first spinner but not sure how to do the second one in java. Here is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    What happens when we access the same variable defined in two interfaces implemented by the same class? (a) ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What are the two types of Streams offered by java 8? (a) sequential and parallel (b) sequential and random ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I have a text file, by which I am trying to write to in my program. Whenever user wants to add ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have to copy classpath resource from one package to another. My program is: public static void main( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Rather than rewriting the entire contents of an xml file when a single element is updated, is there a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 26, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    My application is receiving email through SMTP server. There are one or more attachments in the email and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Which of these class contains the methods used to write in a file? (a) FileStream (b) FileInputStream (c) ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these exception is thrown in cases when the file specified for writing is not found? (a) IOException (b ... & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
...