- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a new empty file in Java
A new empty file with the required abstract path name can be created using the method java.io.File.createNewFile(). This method requires no parameters and it returns true if the file is newly created and it did not exist previously. If the file existed previously, it returns false.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("File: " + file); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
File: demo1.txt
Now let us understand the above program.
The file is created using the method java.io.File.createNewFile(). Then the file name is printed. A code snippet that demonstrates this is given as follows −
try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("File: " + file); } catch(Exception e) { e.printStackTrace(); }
- Related Articles
- How to create a new directory by using File object in Java?
- Golang program to create a new file
- How to Create a New Ext4 File System in Linux?
- How to Create a New File in Linux from Bash?
- How to create an empty file using Python?
- Create a temporary file in Java
- How to create a pdf file in Java?
- Create empty border with BorderFactory class in Java?
- Create a new ArrayList from another collection in Java
- How do you create an empty list in Java?
- Create class Objects in Java using new operator
- How to create a new image from a JPEG file using the imagecreatefromjpeg() function in PHP?
- How to create a Feature file for Cucumber in Java?
- Create temporary file in specified directory in Java
- How to Write/create a JSON file using Java?

Advertisements