Java - mkdir() not writing directory

19,068

Solution 1

It's possibly because File.mkdir creates the directory only if the parent directory exists. Try using File.mkdirs which creates all the necessary directories.

Here's the code which worked for me.

private void writeDir(File f){
    try{
         if(f.mkdirs()) { 
             System.out.println("Directory Created");
        } else {
        System.out.println("Directory is not created");
        }
    } catch(Exception e){
            //  Demo purposes only.  Do NOT do this in real code.  EVER.
            //  It squashes exceptions ...
        e.printStackTrace();
    }
}

The only change I made was to change f.mkdir() to f.mkdirs() and it worked

Solution 2

I think that @La bla bla has nailed it, but just for completeness, here are all of the things that I can think of that could cause a call to File.mkdir() to fail:

  • A syntax error in the pathname; e.g. an illegal character in a file name component
  • The directory to contain the final directory component does not exist.
  • There is already something with that name.
  • You don't have permission to create a directory in the parent directory
  • You don't have permission to do a lookup in some directory on the path
  • The directory to be created is on a read-only file system.
  • The file system gave a hardware error or network related error.

(Obviously, some of these possibilities can be quickly eliminated in the context of this question ...)

Share:
19,068
Nick
Author by

Nick

Web dev is my jam, though I also dabble in Android and iOS native development. Run my own development firm and have been doing web for nearly 10 years now

Updated on June 23, 2022

Comments

  • Nick
    Nick about 2 years

    I am trying to create a directory but it seems to fail every time? I have checked that it is not a permission issue too, I have full permission to write to that directory. Thanks in advance.

    Here is the code:

    private void writeTextFile(String v){
        try{
    
            String yearString = convertInteger(yearInt);
            String monthString = convertInteger(month);
            String fileName = refernce.getText();
            File fileDir = new File("C:\\Program Files\\Sure Important\\Report Cards\\" + yearString + "\\" + monthString);
            File filePath = new File(fileDir + "\\"+ fileName + ".txt");
            writeDir(fileDir);
            // Create file 
            FileWriter fstream = new FileWriter(filePath);
            try (BufferedWriter out = new BufferedWriter(fstream)) {
                out.write(v);
            }
        }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        }
    }
    
    private void writeDir(File f){
        try{
             if(f.mkdir()) { 
                 System.out.println("Directory Created");
            } else {
            System.out.println("Directory is not created");
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public static String convertInteger(int i) {
        return Integer.toString(i);
    }
    
    Calendar cal = new GregorianCalendar();
    public int month = cal.get(Calendar.MONTH);
    public int yearInt = cal.get(Calendar.YEAR);
    

    Here is the output:

    Directory is not created
    Error: C:\Program Files\Sure Important\Report Cards\2012\7\4532.txt (The system cannot find the path specified)
    
  • Nick
    Nick almost 12 years
    Cheers that seems to be the problem, I will accept the answer when it alows me to :) THANKS!!!
  • Stephen C
    Stephen C almost 12 years
    Since the "dynamic" part of the pathname has two directories, I'd say this theory is pretty good ...
  • La bla bla
    La bla bla almost 12 years
    I copied your code, changed it to mkdirs() and it worked. Editing to include relevant code
  • Tony Eastwood
    Tony Eastwood over 9 years
    WARNING FOR WINDOWS7. I just wasted hours discovering that if a file name contains any directory part which is one of Window7 legacy words (that is : CON,PRN,AUX,CLOCK$,NUL,COM1-COM9,LPT1-LPT9... as we all knew... didn't we? ) then mkdirs will fail without warning or apparent logical explanation. Found under Java 7 b51 - it may not affect other versions of Java or other programming languages. It does not affect the DOS cmd shell.