Create a File in Java

 
What are Files ?

Files are a primary source and destination for data within many programs. A File is an abstract representation of file and directory pathnames. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate sub-directory hierarchies.

The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems.

File class defines many methods that obtain the standard properties of a File object. For example, getName( ) returns the name of the file, getParent( ) returns the name of the parent directory and exists( ) returns true if the file exists, false if it does not.

How to create a File ?

There are many ways to create a File in java. Let’s see these one by one.

1) Using java.io.File class:

To create a file using java.io.File class, use File.createNewFile() method from File Class. Let’s see this with an example.

Output:

2) Using java.io.FileOutputStream Class:

Output:

Related posts

Leave a Comment