Search This Blog

Thursday 11 September 2014

Input String and Write into File - JAVA

How to Input String and write to a file in Java - File Handling in java

This program shows how to input a string from user and write it into a file in Java..

Testing_.java

import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;

public class Testing_
{
    public static void main(String args[]) throws FileNotFoundException{
     System.out.print("Enter string to be written to file: ");
     //input
     Scanner scanner = new Scanner(System.in);
     String stringToBeWritten = scanner.nextLine();
     Formatter formatter = new Formatter("myFile.txt");
     formatter.format("THe string I want to write is: %s", stringToBeWritten);
     if (formatter!=null)
      formatter.close();
     System.out.println("Written!");
    }
}

No comments:

Post a Comment