How To Create And Write A Text File In Java

We will see how to create and write a text file in java so basically you get the idea after reading the title whatever when you run this program it creates a txt file and write some text inside so lets start making it , i am doing this in text editor so it's good id you also use a text editor ok now create a class and add the namespace io import java.io.*; and now write down the public static void main(String args[]) by the way don't forget to add the tags ({) now write down FileOutputStream Create; after that write this PrintStream Write; alright i will just put here the whole codes to make it easier for you

import java.io.*;
public class Create_txt 
{ 

 public static void main(String args[])
   {
    FileOutputStream Create;
    PrintStream Write;
    try
   {
    Create = new FileOutputStream("Text.txt");

    Write = new PrintStream( Create );

    Write.println ("Ultimate programming tutorials");

    Write.close();
    } 
    catch (Exception e)
    {
   System.err.println ("Cannot write txt file");
   }
  }
}

now when you run a new txt file will be created named "Text.txt" and Ultimate programming tutorials will be there inside the txt file if something stopped the process you will see "Cannot write txt file" in the console , i hope this is useful.

1 comments:

Post a Comment

Note: Only a member of this blog may post a comment.