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 6175641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:57:38+00:00 2026-05-23T23:57:38+00:00

While using File.mkdir , and friends I notice that they don’t throw exceptions on

  • 0

While using File.mkdir, and friends I notice that they don’t throw exceptions on failure! Thankfully FindBugs pointed this out and now my code at least checks the return value but I still see no way to get meaningful information about why the call fails!

How do I find out why calls to these File methods fail? Is there a good alternative or library that handles this?

I’ve done some searches here on SO and Google and found surprising little info on this topic.

[update] I’ve given VFS a try and its exception don’t have anymore useful information. For example trying to move a directory that had been recently deleted resulted in Could not rename file "D:\path\to\fileA" to "file:///D:/path/do/fileB". No mention that fileA no longer existed.

[update] Business requirements limit me to JDK 1.6 solutions only, so JDK 1.7 is out

  • 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-23T23:57:38+00:00Added an answer on May 23, 2026 at 11:57 pm

    You could call native methods, and get proper error codes that way. For example, the c function mkdir has error codes like EEXIST and ENOSPC. You can use JNA to access these native functions fairly easily. If you are supporting *nix and windows you will need to create two versions of this code.

    For an example of jna mkdir on linux you can do this,

    import java.io.IOException;
    
    import com.sun.jna.LastErrorException;
    import com.sun.jna.Native;
    
    public class FileUtils {
    
      private static final int EACCES = 13;
      private static final int EEXIST = 17;
      private static final int EMLINK = 31;
      private static final int EROFS = 30;
      private static final int ENOSPC = 28;
      private static final int ENAMETOOLONG = 63;
    
      static void mkdir(String path) throws IOException {
    
        try {
          NativeLinkFileUtils.mkdir(path);
    
        } catch (LastErrorException e) {
          int errno = e.getErrorCode();
          if (errno == EACCES)
            throw new IOException(
                "Write permission is denied for the parent directory in which the new directory is to be added.");
          if (errno == EEXIST)
            throw new IOException("A file named " + path + " already exists.");
          if (errno == EMLINK)
            throw new IOException(
                "The parent directory has too many links (entries).  Well-designed file systems never report this error, because they permit more links than your disk could possibly hold. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.");
          if (errno == ENOSPC)
            throw new IOException(
                "The file system doesn't have enough room to create the new directory.");
          if (errno == EROFS)
            throw new IOException(
                "The parent directory of the directory being created is on a read-only file system and cannot be modified.");
          if (errno == EACCES)
            throw new IOException(
                "The process does not have search permission for a directory component of the file name.");
          if (errno == ENAMETOOLONG)
            throw new IOException(
                "This error is used when either the total length of a file name is greater than PATH_MAX, or when an individual file name component has a length greater than NAME_MAX. See section 31.6 Limits on File System Capacity.");
          else
            throw new IOException("unknown error:" + errno);
        }
    
    
    
    
      }
    }
    
    class NativeLinkFileUtils {
      static {
        try {
          Native.register("c");
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      static native int mkdir(String dir) throws LastErrorException;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.