Java create new file

Let’s learn java create new file.

Java create new file

Creating a new file in the java program is very common in java interview question.

In this post we are going to discuss to create a new file using createNewFile() method of java.io.File class which creates a new, empty file and returns a boolean value.

java create new file

In creating a file, first create File object then call createNewFile() method to create new file.

File class createNewFile() method returns true if file is created, else returns false.

If createNewFile() method is not able to create new file then java.io.IOException is thrown.

Also read – how to create directory in java

Let’s see an example on createNewFile() method of File class.

import java.io.File;
import java.io.IOException;
public class CreateFileDemo
{
   public static void main(String[] args) throws IOException
   {
      try
      {
         File file = new File("D:\\demo.txt");
         boolean bool = file.createNewFile();
         if(bool)
         {
            System.out.println("File created successfully");
         }
         else
         {
            System.out.println("File already exists");
         }
      }
      catch(IOException ex)
      {
         System.out.println("Exception : ");
         ex.printStackTrace();
      }
   }
}



Using FileOutputStream.write(byte[] b) method

Here I’m going to show FileOutputStream.write(byte[] b) method.

FileOutputStream.write(byte[] b) method writes b.length bytes from the specified byte arrayto this file output stream.

Let’s see java program on FileOutputStream.write(byte[] b) method.

import java.io.FileOutputStream;
import java.io.IOException;
public class Example
{
   public static void main(String[] args) throws IOException
   {
      String strFileName = "/project/fb/java/new.txt";    
      try(FileOutputStream fos = new FileOutputStream(strFileName)) 
      {
         String strText = "Hello World";
         byte[] bytes = strText.getBytes();            
         fos.write(bytes);
      }
   }
}



create file if not exist

Let’s learn to create file if not exist using FileOutputStream class.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class CreateFileIfNotExist 
{
   public static void main(String[] args) throws IOException
   {
      FileOutputStream fos = null;
      try
      {    
         fos = new FileOutputStream("java/createFile.txt");
      } 
      finally 
      {    
         if(fos != null) 
         {
            fos.close();
         }
      }
   }
}



java program to write into a text file

To write into a text file use FileWriter class. This class Writes text to character files using a default buffer size.

Encoding from characters to bytes uses either a specified charset or the platform’s default charset.

Here’s the java program to write into a text file.

import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteIntoTextFile 
{
   public static void main(String[] args)
   {
      try
      {    
         FileWriter fw = new FileWriter("B:\\demo.txt");    
         fw.write("Hello World");    
         fw.close();    
      }
      catch(Exception ex)
      {
         System.out.println(ex);
      }    
      System.out.println("Successful");
   }
}


Output:

Successful

demo.txt:

Hello World


read file

To read file we are using scanner class. Here’s example on java read file.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class JavaReadFile 
{
   public static void main(String[] args) 
   {
      try
      {
         File obj = new File("fruitnames.txt");
         Scanner read = new Scanner(obj);
         while(read.hasNextLine()) 
         {
            String strData = read.nextLine();
            System.out.println(strData);
         }
         read.close();
      } 
      catch(FileNotFoundException ex) 
      {
         System.out.println("Error occurred.");
         ex.printStackTrace();
      }
   }
}


Output:

apple
orange
banana
pineapple


java delete file

To delete a file in java delete() method of File class is used.

delete() method deletes the file or directory denoted by this abstract pathname.

If this pathname denotes a directory, then the directory must be empty inorder to be deleted.

delete() method returns true if and only if the file or directory is successfully deleted; false otherwise.

Here’s the program on delete a file in java.

import java.io.File;
public class JavaDeleteFile
{
   public static void main(String[] args) 
   {
      try
      {         
         File file = new File("E:\\example.txt");  
         if(file.delete())  
         {
            System.out.println(file.getName() + " deleted");  
         }
         else  
         {  
            System.out.println("failed");  
         }
      }  
      catch(Exception ex)  
      {  
         ex.printStackTrace();  
      }
   }
}