Solved programming problem

Write in a .txt File with Print Writer in Java.

Source code

package writer;

import java.io.FileReader;

import java.io.PrintWriter;

import java.io.IOException;

import java.util.Scanner;

public class Writer

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        PrintWriter writer =null;

       

        System.out.println("Enter Massege: ");

        String msg= sc.nextLine();

       

        try

        {

            writer = new PrintWriter("Output.txt"); ///create + open file

            writer.write(msg);///write on the file

            writer.close();///close the file

        }

        catch(IOException e)

        {

            System.out.println("Cant Create A file");

        }

    }

   

}

Output

Enter Message:

Code to HaveN

.txt File

 

 

Join the discussion

Add your comment