Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6997615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:18:21+00:00 2026-05-27T20:18:21+00:00

I want to check if a given String is a file or a directory,

  • 0

I want to check if a given String is a file or a directory, i’ve tried the methods isFile() and isDirectory() of the File class but the problem is that if the directory or file doesn’t exist these methods returns false, because as stated in the javadoc :

isFile() :

true if and only if the file denoted by this abstract pathname exists
and is a normal file; false otherwise

isDirectory() :

true if and only if the file denoted by this abstract pathname exists
and is a directory; false otherwise

Basically i need two methods without the exist clause …

So i want to test if the given string complies to a directory format or complies to a file format, in a multiplatform context (so, should work on Windows, Linux and Mac Os X).

Does exist some library that provide these methods ? What could be the best implementation of these methods ?

UPDATE

In the case of a string that could be both(without extension) by default should be identified as directory, if a file with that path does not exist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:18:22+00:00Added an answer on May 27, 2026 at 8:18 pm

    Sounds like you know what you want, according to your update: if the path doesn’t exist and the path has an extension it’s a file, if it doesn’t it’s a directory. Something like this would suffice:

    private boolean isPathDirectory(String myPath) {
        File test = new File(myPath);
    
        // check if the file/directory is already there
        if (!test.exists()) {
            // see if the file portion it doesn't have an extension
            return test.getName().lastIndexOf('.') == -1;
        } else {
            // see if the path that's already in place is a file or directory
            return test.isDirectory();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check if a given string appears multiple times in some scenarios
I have a function that checks for file and directory changes within a given
I want to check if a given date is more than a month earlier
I want to check for the existence of a table with a given ID
I want to check for duplicated words right next to each other, but even
I want to check when the user double click on applictaion icon that no
Code: String dir = //Path to the directory File saveDir = new File(dir); //Here
I have a method which is used to check if the given class is
I want to make sure a file path set via query string does not
What I need is to check whether a given string partially matches a given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.