All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create A Text File In Java

Last Updated : Mar 11, 2024

How To Create A Text File In Java

In this article we will show you the solution of how to create a text file in java, in case you using eclipse then each program adds default package or basic package itself at the first line.

To achieve desired result everyone need to import package of ‘File and IOException’ because these supports file creation some block of code.

The createNewFile() method can help you to reach the target if not exception will printed on output block.

Step By Step Guide On How To Create A Text File In Java :-

We named main class ‘Ctxtfile’, there we provide definition for public static main method.

Where you have can create a file location input need with file name. Boolean type value created for analyze state of text file creation at if method and printing message appropriate with file path that is retrieved by ‘getCanonicalPath()’ method.

If file creation faced any difficulties at that time exception printed with tracking detail as output to rectify it.

//Create Text File
package java_prgms;
import java.io.File;
import java.io.IOException;
class Ctxtfile_prgm
{
 public static void main(String args[])
    {
  File f=new File("C:\\Users\\welcome\\Desktop\\1.txt");
  boolean res;
  try{
   res=f.createNewFile();
   if(res){
    System.out.println("file created "+f.getCanonicalPath());//returns the path string
   }
   else{
    System.out.println("File exist at location: "+f.getCanonicalPath());
   }
  }
  catch(IOException e){
   e.printStackTrace();//prints exception if any
  }
     }
}
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First displaying package will inserted by eclipse to support java codes, if you try codes in eclipse platform each time it will imported after successful creation of java project.
  3. In main class ‘Ctxtfile_prgm’, ensure whether you are given name for class and saved program name same or not. If same you do not get confuse at any case during execution otherwise name for execution same for both run and compiling comment.
  4. Within main class we provide main method definition then as foremost step we creating File object ‘f’ with desired file path with name of the text file need to create and created Boolean variable ‘res’.
  5. In the try block using createNewFile() method we successfully creating new text file at the given location by binding file object of ‘f’ with it.
  6. Using if() we checking whether file successfully created or not if yes then it prints output text ‘file created’ with path that is obtain by ‘f.getCanonicalPath()’.
  7. If you checking or compiling it second time it will throw message ‘File exist at location’ with location otherwise error may occurred at creation, so exception prints exact where problem happened details.
  8. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  9. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to create a new text file in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac Ctxtfile_prgm.java’.

After successful compilation you need to execute ‘java Ctxtfile_prgm‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output ‘file created C:\\Users\\welcome\\Desktop\\1.txt’.

I hope this article on how to create a text file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪