java

Java Program to Write into a Text File

In this tutorial, we are going to see how to write into a text file in Java. We use the FileWriter class with its write() method to write text to the file we created in the previous tutorial. Note that when you have finished writing to the file, you must close it with the close() method:
 

Java Program to Write into a Text File
import java.io.*; 

public class Main {
  public static void main(String[] args) {
    try {
      FileWriter fw = new FileWriter("NewFile.txt");
      fw.write("Hello, Welcome to StackHowTo!");
      fw.close();
      System.out.println("The text has been written successfully");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

The text has been written successfully
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *