Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - mkdir() not writing directory

Tags:

java

mkdir

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)
like image 609
Nick Avatar asked Aug 26 '12 01:08

Nick


2 Answers

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

like image 140
La bla bla Avatar answered Sep 30 '22 16:09

La bla bla


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 ...)

like image 39
Stephen C Avatar answered Sep 30 '22 15:09

Stephen C