Create file

April 11, 2016, by Hamid Siddiqui

/**
 * @author Hamid Siddiqui
 * https://www.quora.com/profile/Hamid-Siddiqui
 * https://github.com/inventionsbyhamid/
 *
 * @program To create a new file
 */

import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class NewFile {

    public static void main(String args[]) throws IOException {

        BufferedReader k = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter file path");
        //  C:\Users\test.txt
        String path = k.readLine();
        try {
            Files.createFile(Paths.get(path));
            System.out.println("File created successfully");
        }
        catch(FileAlreadyExistsException e) {
            System.out.println("Error, File already exists");
        }
        catch(SecurityException e){
            System.out.println("Error, No permission to create file in entered location");
        }
        catch(Exception e) {
            System.out.println("Error "+e.getMessage());
        }

    }
}
Sample Input/Output

Enter file path
E:\Hamid\test.txt
File created successfully




Factorial Recursion →

← File Existence