Methods to Create a File in a Specified Directory
Three such methods to create a file in a specified directory are mentioned below:
- File.createNewFile() method
- FileOutputStream class
- File.createFile() method
While doing development it is essential to create files, read the files, and perform many other operations. Therefore it becomes very crucial to learn how files can be created in a specific directory.
1. File.createNewFile() Method
This is a very simple method to create a file in a specific directory using Java. On creating a file successfully it returns true and if the file is not created due to an error it will return false. For exception handling (To avoid IOException) we will use try-catch block in the program.
Below is the implementation of the mentioned method:
Java
// Java Progrm to Create a File in a Specified Directory // Using File.createNewFile() Method import java.io.File; import java.io.IOException; // Driver Class public class GFG { // main function public static void main(String[] args) { String directoryPath = "C:/GFG/Java createfile/" ; String fileName = "newfile.txt" ; File file = new File(directoryPath + fileName); try { // File.createNewFile() Method Used boolean isFileCreated = file.createNewFile(); if (isFileCreated) { System.out.println( "File created successfully." ); } else { System.out.println( "File already exists or an error occurred." ); } } catch (IOException e) { e.printStackTrace(); } } } |
Output:
File created successfully.
After successfully running the code the file will be created in specified directory.
Screenshot Showing the File Created:
2. FileOutputStream Class
The second method to create a file is using FileOutputStream Class. Using this method we get more control over the file in terms of content.
Below is the implementation of the mentioned method:
Java
// Java Progrm to Create a File in a Specified Directory // Using FileOutputStream Class import java.io.FileOutputStream; import java.io.IOException; public class GFG { public static void main(String[] args) { String directoryPath = "C:/GFG/Java createfile/" ; String fileName = "newfile2.txt" ; try { //Open file // FileOutputStream Class Used FileOutputStream fileOutputStream = new FileOutputStream(directoryPath + fileName); // Write data to the file if needed. //Close file fileOutputStream.close(); System.out.println( "File created successfully." ); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
File created successfully.
Screenshot Showing the File Created:
3. File.createFile() Method
Using java.nio.file package this method is alternative to create a file. More file handling capabilities are provided by this method.
Below is the implementation of the mentioned method:
Java
// Java Progrm to Create a File in a Specified Directory // Using File.createFile() Method import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; // Driver Class public class GFG { // main function public static void main(String[] args) { String directoryPath = "C:/GFG/Java createfile/" ; String fileName = "newfile3.txt" ; Path filePath = Paths.get(directoryPath, fileName); try { // File.createFile() Method Used Files.createFile(filePath); System.out.println( "File created successfully." ); } catch (FileAlreadyExistsException e) { System.out.println( "File already exists." ); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
File created successfully.
Screenshot Showing the File Created:
Java Program to Create a File in a Specified Directory
Creating a file in a specific directory using Java can be done in various ways. This is done using predefined packages and Classes. In this article, we will learn about how to create a file in a specified directory.