Java: How to create and write a Text file

Leave a Comment

How to create and write a Text file

 The program code

import java.io.*;

public class WriteTextFileExample{
  public static void main(String[] args)throws IOException{
  Writer output = null;
  String text = "My text file";
  File file = new File("write.txt");
  output = new BufferedWriter(new FileWriter(file));
  output.write(text);
  output.close();
  System.out.println("Your file has been written"); 
  }
}

After running the above code it will create a text file in the projects folder inside the netbeans folder in the documents folder.
 The text file file will have the text My text file



0 comments:

Post a Comment