Return a file with the given filename creating the necessary directories if not present in Java

Description

The following code shows how to return a file with the given filename creating the necessary directories if not present.

Example


//from w ww  . j  av a  2  s.  c  o m
import java.io.File;



public class Main {
  public static File createFile(File destDir, String filename) {
      File file = new File(destDir, filename);
      File parent = file.getParentFile();
      if (parent != null)
          parent.mkdirs();
      return file;
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip