Create File Java Program

Sharing is healthy !!

Hey guys!! Welcome to flower brackets blog. Today we are going to learn create file java program.

To create file java we will be using two classes, that is, File class and IOExceptionclass of java inputoutput package.

Then in new File instance we will be converting the given pathname string into an abstract pathname.

The file class instance method, createNewFile(), creates new file if not created. Let’s take a look on java create text file,

Example: Create File Java Program

Create File Java Program

import java.io.File;
import java.io.IOException;

public class CreateFile
{
   public static void main(String[] args)
   {
      try
      {
         File fl = new File("C:/createfile.txt");
         if(fl.createNewFile())
         {
            System.out.println("File created");
         }
         else
         {
            System.out.println("File exists");
         }
      }
      catch(IOException ioe)
      {
         ioe.printStackTrace();
      }
   }
}

Output:

File created

using formatter

Here I’m going to show you how to create a new file in java using formatter. So the first thing you need to do is import java.util package.

Now let’s go ahead and see how to create files in java,

import java.util.*;

public class CreateFile
{
   public static void main(String[] args)
   {
      final Formatter createFile;
 
      try
      {
         createFile = new Formatter("learnjava.txt");
         System.out.println("File created!!");
      }
      catch(Exception ee)
      {
         System.out.println("Error!!");
      }
   }
}

Output:

File created!!

What I have done here is, I have built a Formatter variable. So for that we need to make a final variable as shown above.

This formatter variable outputs string to a file and it’s like “printf”. But instead of printing to the screen you print a file.

So this outputs a string. I have used exception handling.

Next we are setting “createFile” is equal to new Formatter(). This is same as instantiating the variable in the same line.

But we want to try to do it in “try” block as the constructor. It takes the path of the file.

You can also read – Create Directory Java Program

I’m going to make a file like “learnjava.txt”. Here what we have to understand is, if there is no file to output to, then automatically creates a file on your computer for you.

Now that we have created file we want something to show up in the command prompt that tells us that we did it.

Here you have created a file in whatever directory your program ran from. So if you have java program right in your hard drive then it’s going to create this text file right on your hard drive.

For example, If you run this java program from a folder called “File” then it’s going to create this text file in the folder called “File”.

So, that is how you can create text file java on your computer using formatter.


conclusion

That’s it guys. This is all about how to create a text file in java. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Sharing is healthy !!