Create a file

ReturnMethodSummary
booleancreateNewFile()Creates a new file.
static FilecreateTempFile(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 FilecreateTempFile(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);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

The output:

c:\abc8669897675133345122txt
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.