Home >>Java Tutorial >Java Create Files

Java Create Files

Java Create and Write To Files

This tutorial will help you in understanding the Java Create and Write To Files. The programmers can create and write a file in the Java language.

Create a File

In order to create a file in Java the programmers can use the createNewFile() method. This method is generally used as it returns a boolean value: true in case the file was created successfully and it returns false in case the file already exists.

Please note that the method is always enclosed in a try...catch block and it is mandatory as of the fact that it throws an IOException in case of an error that simply translates to the fact that if the file is not created for some unknown.

Here is an example that will explain you the physical aspect of it:

import java.io.File;//First Import the File class
import java.io.IOException;//Now to handle errors Import the IOException class
public class MyFile {
  public static void main(String[] args) {
    try {
      File Obj = new File("myfile.txt");
      if (Obj.createNewFile()) 
	  {
        System.out.println("Conogrates File created Successfully " + Obj.getName());
      } 
	  else 
	  {
        System.out.println("This File already exists.");
      }
    } catch (IOException e) 
	{
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}
Output :Conogrates File created Successfully myfile.txt

In order to create a file in a specific directory that generally requires permission the programmer have to specify the path of the file and then use the double backslashes in order to escape the "\" character in case of Windows.

If you are using Mac and Linux then the programmer can simply write the path: /Users/name/filename.txt

Here is an example of the same that is depicted below for the deeper understanding of the topic:

File Obj = new File("C:\\Users\\MyName\\myfilename.txt");

Write To a File

Let's understand the write to a file in Java by the help of an example that is depicted below. The programmer have used the FileWriter class together with its write () method in order to write some text to the file that has been created earlier in this tutorial.

Please Note that the file needs to be closed with the close () method when the programmer is done writing to the file.

Here is an example of the write To a file in Java language that is depicted below:

import java.io.FileWriter; //First need to  Import the FileWriter class
import java.io.IOException;//To handle errors Import the IOException class 
public class WriteFile{
  public static void main(String[] args) 
  {
    try {
      FileWriter obj = new FileWriter("myfile.txt");
      myWriter.write("Welcome to the world of File Handling");
      myWriter.close();
      System.out.println("Congrates ! Successfully wrote to the file.");
    } catch (IOException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}
Output :Congrates ! Successfully wrote to the file.

Java Read Files

To read the contents of the text files use the Scanner class

In this example we read the content from myfile.txt created in previous example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile{
  public static void main(String[] args) {
    try {
      File Obj = new File("myfile.txt");
      Scanner myReader = new Scanner(Obj);
      while (myReader.hasNextLine()) 
	  {
        String myData = myReader.nextLine();
        System.out.println(myData);
      }
      myReader.close();
    } catch (FileNotFoundException e) {
      System.out.println("An error occurred.");
      e.printStackTrace();
    }
  }
}
Output :Welcome to the world of File Handling

How to Delete a Files

delete() method is used to delete a file in Java

Here is an example to delete Java Files

import java.io.File;  // Import the File class

public class DeleteFile {
  public static void main(String[] args) { 
    File myObj = new File("myfile.txt"); 
    if (myObj.delete()) { 
      System.out.println(myObj.getName()+" Files deleted successfully");
    } else {
      System.out.println("Failed to delete the file.");
    } 
  } 
}
Output :myfile.txt Files deleted successfully

How to Delete a Folder

Here is an example to delete a folder(Directory)

import java.io.File; 

public class DeleteFolder {
  public static void main(String[] args) { 
    File myObj = new File("C:\\Users\\MyName\\Demo"); 
    if (myObj.delete()) 
	{ 
      System.out.println(+ myObj.getName()+" Directory/folder deleted successfully");
    } 
	else 
	{
      System.out.println("Failed to delete the folder.");
    } 
  } 
}
Output :Demo Directory/folder deleted successfully