Create a file

boolean createNewFile()
Creates a new file.
static File createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
static File createTempFile(String prefix, String suffix, File directory)
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

import java.io.File;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    File aFile = new File("c:/aFolder");
    
    try {
      System.out.println(aFile.createNewFile());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

The following code creates a temporary file under user's home directory.


import java.io.File;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    
    try {
      File tempFile = File.createTempFile("abc","txt");
      System.out.println(tempFile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

The code below creates a temporary file with the directory you passed in:


import java.io.File;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    
    try {
      File tempFile = File.createTempFile("abc","txt",new File("c:\\"));
      System.out.println(tempFile);//c:\abc8669897675133345122txt
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
Home 
  Java Book 
    File Stream  

File:
  1. File
  2. Constants from File class
  3. Create file object
  4. Is it executable, readable or writable
  5. Compare two file path
  6. Create a file
  7. Delete a file
  8. Is it a file or a directory
  9. Whether the file or directory denoted by this abstract pathname exists
  10. Whether this abstract pathname is absolute
  11. Is this file hidden
  12. Get the file last modified time
  13. Get the file size
  14. Get file path and name
  15. Get free space, total space, usable space
  16. Get parent file
  17. Return file in a directory
  18. The file system roots
  19. Create new directories
  20. Rename file
  21. Change to executable, readable, writable
  22. Change last-modified time
  23. Get string representation of a file location
  24. Convert file location to URI and URL