Write File in Java




The below given code used to write the file.First Declare the BufferedWriter object("write_file") then write"c c++ java python".compile and run the program.then see the File("file.txt") you will see the output like this("c c++ java python").







Example
import java.io.*;
import java.util.*;
public class Main
{
     public static void main(String[] args) throws Exception
     {
          try
          {
                BufferedWriter write_file = new BufferedWriter(new FileWriter("file.txt"));
                                                       
                write_file.write("C \n");
                write_file.write("C++\n");
                write_file.write("Java\n");
                write_file.write("Python\n");

                write_file.close();
          }

          catch(Exception e)
          {
                return;
          }
     } 
}







Explain




  1. In java programming , file writing very simple.
  2. First Declare the BufferedWriter object("write_file") then write"c c++ java python".
  3. compile and run the program.then see the File("file.txt") you will see the output like this("c c++ java python").
  4. try and catch block used for handling the Exception.




Download the code