My Simple code to write to file -- Java

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

My Simple code to write to file -- Java

Post by Nipuna » Fri Apr 13, 2012 9:19 am

Hi

I wrote a little program to write to a text file. This is a very simple one and I wrote it all by myself.

I thought this would be useful to a beginner in Java since this code is a very simple one.

So here goes the code.

Code: Select all

import java.util.Formatter;
import java.util.Scanner;

public class Write_To_File{
	
	public static void main(String args[]){
		
	
		try{
			Formatter file = new Formatter("Output.txt");
			Scanner input = new Scanner(System.in);
			System.out.println();
			System.out.println("Enter what you need to write to the file");
			file.format("%s ", input.nextLine());
			file.close();
		}
		catch(Exception e){}
	}
}
Post Reply

Return to “Java Programming”