Java — How to create a new file | Code Factory

Create New file using Java NIO (Recommended) — JDK 7+

package com.example.java.programming.file;import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class CreateNewFile {
public static void main(String... args) {
Path filePath = Paths.get("./test.txt");
try {
// Create a file at the specified file path
Files.createFile(filePath);
System.out.println("File created successfully!");
} catch (FileAlreadyExistsException e) {
System.out.println("File already exists");
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
System.out.println("No permission to create file: " + e.getMessage());
}
}
}

Create New File with missing parent directories using Java NIO

package com.example.java.programming.file;import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author code.factory
*
*/
public class CreateNewFile {
public static void main(String... args) {
Path filePath = Paths.get("files/file/test.txt");

try {
// Create missing parent directories
if (filePath.getParent() != null) {
Files.createDirectories(filePath.getParent());
}
// Create a file at the specified file path
Files.createFile(filePath);
System.out.println("File created successfully!");
} catch (FileAlreadyExistsException e) {
System.out.println("File already exists");
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
System.out.println("No permission to create file: " + e.getMessage());
}
}
}

Create New File in Java using java.io.File class — JDK 6+

  • true, if the file does not exist and was created successfully
  • false, if the file already exists
package com.example.java.programming.file;import java.io.File;
import java.io.IOException;
/**
* @author code.factory
*
*/
public class CreateNewFile {
public static void main(String... args) {
File file = new File("./test.txt");
try {
// Create the file in the filesystem
boolean success = file.createNewFile();
if (success) {
System.out.println("File created successfully!");
} else {
System.out.println("File already exists!");
}
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
System.out.println("No sufficient permission to create file: " + e.getMessage());
}
}
}

Create New File along with missing parent directories with java.io.File class

package com.example.java.programming.file;import java.io.File;
import java.io.IOException;
/**
* @author code.factory
*
*/
public class CreateNewFile {
public static void main(String... args) {
File file = new File("files/file//test.txt");
try {
// Create missing parent directories
if (file.getParentFile() != null) {
file.getParentFile().mkdirs();
}
// Create the file
boolean success = file.createNewFile();
if (success) {
System.out.println("File created successfully!");
} else {
System.out.println("File already exists!");
}
} catch (IOException e) {
System.out.println("An I/O error occurred: " + e.getMessage());
} catch (SecurityException e) {
System.out.println("No sufficient permission to create file: " + e.getMessage());
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a Web Service WhatsApp Cloud API & Flask: Sending Template Messages — Part 1

screenshot-developers.facebook.com-2022.05.24-22_27_03.png

My Favorite SQL Prompt Features

AWS Serverless or Lambda

Postgres replica conflicts: Part 1

How to determine the scope of an Integration Project

Setting up a Node/Express app with HTTPS

Class and instance attributes

Successfully Implement a Cobot into your Operations: Step 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Key Differences Between Spring Boot and Spring Framework

Functional Programming in Java

Java Threads

Database Sharding