217

The condition is if the directory exists it has to create files in that specific directory without creating a new directory.

The below code only creates a file with the new directory but not for the existing directory . For example the directory name would be like "GETDIRECTION":

String PATH = "/remote/dir/server/";
    
String fileName = PATH.append(id).concat(getTimeStamp()).append(".txt");  
             
String directoryName = PATH.append(this.getClassName());   
              
File file  = new File(String.valueOf(fileName));

File directory = new File(String.valueOf(directoryName));

if (!directory.exists()) {
        directory.mkdir();
        if (!file.exists() && !checkEnoughDiskSpace()) {
            file.getParentFile().mkdir();
            file.createNewFile();
        }
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(value);
bw.close();

kohane15
  • 809
  • 12
  • 16
Sri
  • 2,193
  • 2
  • 11
  • 5

9 Answers9

290

Java 8+ version:

Files.createDirectories(Paths.get("/Your/Path/Here"));

The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Inês Gomes
  • 4,313
  • 1
  • 24
  • 32
  • 2
    What happens if permission is required to create ? – Ajay Takur Jul 14 '20 at 09:35
  • 1
    For Android It only works on API 26 and up so Make sure to check this line if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) https://developer.android.com/reference/java/nio/file/Files#createDirectories(java.nio.file.Path,%20java.nio.file.attribute.FileAttribute%3C?%3E...) – Arpit Patel Oct 05 '20 at 13:19
  • @AjayTakur If you don't have write permission in the directory you're trying to create the new directory in, it throws an IOException – Thor Lancaster Dec 12 '20 at 03:40
  • How can make a process get the permission to create directory on Windows 10? – Park JongBum Sep 16 '21 at 21:12
  • What's the performance of this like - can I use it before every file write? – Annan Yearian Feb 03 '23 at 13:33
  • But it throws an `FileAlreadyExistsException` if the path also includes a filename (ends with a filename). – Ola Ström Mar 24 '23 at 15:33
234

This code checks for the existence of the directory first and creates it if not, and creates the file afterwards. Please note that I couldn't verify some of your method calls as I don't have your complete code, so I'm assuming the calls to things like getTimeStamp() and getClassName() will work. You should also do something with the possible IOException that can be thrown when using any of the java.io.* classes - either your function that writes the files should throw this exception (and it be handled elsewhere), or you should do it in the method directly. Also, I assumed that id is of type String - I don't know as your code doesn't explicitly define it. If it is something else like an int, you should probably cast it to a String before using it in the fileName as I have done here.

Also, I replaced your append calls with concat or + as I saw appropriate.

public void writeFile(String value){
    String PATH = "/remote/dir/server/";
    String directoryName = PATH.concat(this.getClassName());
    String fileName = id + getTimeStamp() + ".txt";

    File directory = new File(directoryName);
    if (! directory.exists()){
        directory.mkdir();
        // If you require it to make the entire directory path including parents,
        // use directory.mkdirs(); here instead.
    }

    File file = new File(directoryName + "/" + fileName);
    try{
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(value);
        bw.close();
    }
    catch (IOException e){
        e.printStackTrace();
        System.exit(-1);
    }
}

You should probably not use bare path names like this if you want to run the code on Microsoft Windows - I'm not sure what it will do with the / in the filenames. For full portability, you should probably use something like File.separator to construct your paths.

Edit: According to a comment by JosefScript below, it's not necessary to test for directory existence. The directory.mkdir() call will return true if it created a directory, and false if it didn't, including the case when the directory already existed.

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48
  • the calls are working fine. WHen i tried above piece of coed it is still writing the file to PATH but not to the directory . I have used the File.seperator for the new File creation. – Sri Mar 09 '15 at 19:38
  • Please explain exactly (with classnames and sample variables) what output you are expecting. I've attached my example program in full here http://pastebin.com/3eEg6jQv so you can see that it does what you are describing (as best as I understand). – Aaron D Mar 09 '15 at 19:45
  • 1
    File file = new File(directoryName + "/" + fileName); i replaced the code snippet above with StringBuffer fullFilePath = new StringBuffer(directoryName).append(File.separator).append(fileName); File file = new File(String.valueOf(fullFilePath)); and it worked – Sri Mar 09 '15 at 20:45
  • In that case you can use the [`mkdirs()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs%28%29) method. – Aaron D Jan 10 '17 at 12:34
  • 5
    Why do you have to check for the directory's existence? I played around with this and as far as I can see it does not seem to make a difference, if I create the same directory twice. Even contained files won't get overwritten. Am I missing something? – JosefScript Jan 11 '17 at 15:37
  • No, you're probably not. The problem as asked was to 'create a directory if it doesn't exist', so my code is reflecting that logic, but the JVM's implementation of that call probably won't destroy data if you tell it to create a directory that already exists, so you can call it without the check. If the directory is actually created, the call will return `true`, otherwise `false`. I'll add a note to that effect, thank you. – Aaron D Jan 12 '17 at 00:20
  • 1
    As far as I know, Java (at least Java 8) is now happy with forward slashes are directory separators in both Windows and (obviously) Linux. – Jared Nov 14 '20 at 03:08
35

Trying to make this as short and simple as possible. Creates directory if it doesn't exist, and then returns the desired file:

/** Creates parent directories if necessary. Then returns file */
private static File fileWithDirectoryAssurance(String directory, String filename) {
    File dir = new File(directory);
    if (!dir.exists()) dir.mkdirs();
    return new File(directory + "/" + filename);
}
Jacob R
  • 1,243
  • 1
  • 16
  • 23
29

I would suggest the following for Java8+.

/**
 * Creates a File if the file does not exist, or returns a
 * reference to the File if it already exists.
 */
public File createOrRetrieve(final String target) throws IOException {
  final File answer;
  Path path = Paths.get(target);
  Path parent = path.getParent();
  if(parent != null && Files.notExists(parent)) {
    Files.createDirectories(path);
  }
  if(Files.notExists(path)) {
    LOG.info("Target file \"" + target + "\" will be created.");
    answer = Files.createFile(path).toFile();
  } else {
    LOG.info("Target file \"" + target + "\" will be retrieved.");
    answer = path.toFile();
  }
  return answer;
}

Edit: Updated to fix bug as indicated by @Cataclysm and @Marcono1234. Thx guys:)

Pytry
  • 6,044
  • 2
  • 37
  • 56
  • 1
    `Files.createFile(Files.createDirectories(path)).toFile()` should be `Files.createDirectories(path).toFile()` for `Access Denied` reason. – Cataclysm Sep 11 '18 at 04:51
  • 2
    @Pytry, `Files.createFile(Files.createDirectories(path))` does not work as described in the comment above. `createDirectories` already creates a directory with the file name, e.g. "test.txt", therefore `createFile` will fail. – Marcono1234 Mar 15 '19 at 12:12
10

Simple Solution using using java.nio.Path

public static Path createFileWithDir(String directory, String filename) {
        File dir = new File(directory);
        if (!dir.exists()) dir.mkdirs();
        return Paths.get(directory + File.separatorChar + filename);
    }
Saikat
  • 14,222
  • 20
  • 104
  • 125
9

code:

// Create Directory if not exist then Copy a file.


public static void copyFile_Directory(String origin, String destDir, String destination) throws IOException {

    Path FROM = Paths.get(origin);
    Path TO = Paths.get(destination);
    File directory = new File(String.valueOf(destDir));

    if (!directory.exists()) {
        directory.mkdir();
    }
        //overwrite the destination file if it exists, and copy
        // the file attributes, including the rwx permissions
     CopyOption[] options = new CopyOption[]{
                StandardCopyOption.REPLACE_EXISTING,
                StandardCopyOption.COPY_ATTRIBUTES

        };
        Files.copy(FROM, TO, options);


}
muneebShabbir
  • 2,500
  • 4
  • 29
  • 46
Mukesh
  • 91
  • 1
  • 2
1

If you create a web based application, the better solution is to check the directory exists or not then create the file if not exist. If exists, recreate again.

    private File createFile(String path, String fileName) throws IOException {
       ClassLoader classLoader = getClass().getClassLoader();
       File file = new File(classLoader.getResource(".").getFile() + path + fileName);

       // Lets create the directory
       try {
          file.getParentFile().mkdir();
       } catch (Exception err){
           System.out.println("ERROR (Directory Create)" + err.getMessage());
       }

       // Lets create the file if we have credential
       try {
           file.createNewFile();
       } catch (Exception err){
           System.out.println("ERROR (File Create)" + err.getMessage());
       }
       return  file;
   }
Dr. X
  • 2,890
  • 2
  • 15
  • 37
1

A simple solution using Java 8

public void init(String multipartLocation) throws IOException {
    File storageDirectory = new File(multipartLocation);

    if (!storageDirectory.exists()) {
        if (!storageDirectory.mkdir()) {
            throw new IOException("Error creating directory.");
        }
    }
}
1

If you're using Java 8 or above, then Files.createDirectories() method works the best.

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25
BitsNcode
  • 21
  • 4