You are here: Home / Topics / Program to create a text file using FileWriter and write text into it in Java

Program to create a text file using FileWriter and write text into it in Java

Filed under: Java on 2024-03-01 06:43:43

// Program to create a text file using FileWriter and write text into it.

import java.io.*;

class FileWriterDemo
{
public static void main( String args[ ] ) throws IOException
{
 // attach DataInputStream to keyboard.
 DataInputStream dis = new DataInputStream( System.in );
 
 // attach file to FileWriter
 FileWriter fw = new FileWriter( "file1.txt");
 
 // read characters from keyboard and write it to file.
 char ch;
 System.out.println(" Enter string ( $ to end ) : ");
 while( (ch = (char)dis.read()) != '$' )
 {
  fw.write( ch );
 }

 // close the file.
 fw.close();
}
}


Output:

Enter string ( $ to end ) : 
Learn Programming from www.example.com

About Author:
G
Geetam     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.