java files

How to create a file in java

You can create new file using

  • java.nio.file.Files –> createFile method (New approach since 1.7 JDK)
  • java.io.File –> createNewFile method (Old approach)

public static Path createFile(Path path, FileAttribute… attrs) throws IOException

Creates a new and empty file, failing if the file already exists. The check for the existence of the file and the creation of the new file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the directory.

The attrs parameter is optional file-attributes to set atomically when creating the file. Each attribute is identified by its name. If more than one attribute of the same name is included in the array then all but the last occurrence is ignored.

Parameters:
path – the path to the file to create
attrs – an optional list of file attributes to set atomically when creating the file

Returns:
the file

Throws:
UnsupportedOperationException – if the array contains an attribute that cannot be set atomically when creating the file
FileAlreadyExistsException – if a file of that name already exists (optional specific exception)
IOException – if an I/O error occurs or the parent directory does not exist
SecurityException – In the case of the default provider, and a security manager is installed, the checkWrite method is invoked to check write access to the new file.

package com.candidjava.filenio;


import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateFile {
  public static void main(String[] args) throws IOException {
    Path path=Paths.get("F:/Filesnio/create.txt");
    try {
      Path createdfile=Files.createFile(path);
      System.out.println("File Created Successfully");
      
    } catch (FileAlreadyExistsException e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }

}

java.io.File –> createNewFile method (Old approach)

public boolean createNewFile() throws IOException

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Returns:
true if the named file does not exist and was successfully created; false if the named file already exists

Throws:
IOException – If an I/O error occurred
SecurityException – If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file

package com.candidjava.file;

import java.io.File;
import java.io.IOException;
public class CreateFile {
  public static void main(String[] args) {
    // Using createNewFile() method
    File file = new File("F:/Files/test1.txt");
    boolean result;
    try {
      result = file.createNewFile();
      if (result) {
        System.out.println("File created successfully");
      } else {
        System.out.println("Error in file creation");
      }
    } catch (IOException e) {
      // TODO: handle exception
      e.printStackTrace();
    }

  }

}