All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Write To A File In Java

Last Updated : Jul 1, 2023

How To Write To A File In Java

In this article we will show you the solution of how to write to a file in java, to achieve result you need two more package support that is FileWriter and IOException.

The FileWriter package allow you to create file writer object to successfully written on text file. The IOException package helps you to show the occurred exception in the class.

Now you can write on file text error-free if anything goes wrong exception catch the error and notify you.

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

In main class ‘WtF_prgm’, we provide definition for public static main method. There you should create file writer object as foremost step with argument of destiny file path.

In the file path you must provide two slashes to pointout the exact file. Then you can access write method through file writer object with some text input.

It will inserted to the desired text file then we closed object by close() method. If not catch block obtain some exceptions that will be printed on output to notify user.

//Write to text file
package java_prgms;
import java.io.FileWriter;
import java.io.IOException;
class WtF_prgm
{
 public static void main(String args[])throws Exception
    {
  try{
   FileWriter wrt=new FileWriter("C:\\Users\\welcome\\Desktop\\ff.txt");
   wrt.write("This is new line added to existing file");
   wrt.close();
   System.out.println("Successfully done");
   }
  catch(IOException e){
   System.out.println("Error Occurred.");
   e.printStackTrace();
  }
     }
}
  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. The other two package is must, so remember to import them and get perfect result. Otherwise it cannot understand file writer object creation, write() method like anything, so you get error.
  4. We named main class ‘WtF_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 during execution you have to carefully give excution commend with appropriate name.
  5. With the main class you have to extend IOException to figure out exceptions and handle properly. There first we defined file writer object 'wrt' to written on any file.
  6. Now we bind that object 'wrt' with write() method with text 'This is new line added to existing file'. Instead of this text you can give any alter text based on your need. This write() method successfully write text to file 'ff.txt'.
  7. One more thing is ensures you given file path is correct otherwise you cannot see recently written text on file 'ff.txt'. If written correctly then you will get output of success message otherwise catch blocks print statement will display with occurred exception as output.
  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 write to text file in java. To compile java program you need to tell the java compiler which program to compile through ‘javac WtF_prgm.java’.

After successful compilation you need to execute ‘java WtF_prgm‘ line for gets the output. When you run the program by simply clicks on run button on eclipse will display output ‘Successfully done'.

Then refresh and check ff.txt file you can see you inserted text in the file.

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