001package bradleyross.demonstrations; 002/** 003* Provides a quick demonstration of writing to a file. 004* <p>If the java class is called with parameters, the first 005* parameter is used as the name of the file to be 006* written to.</p> 007* <p>This program also provides an example of how to print 008* time stamps.</p> 009* @author Bradley Ross 010*/ 011public class WriteToFile 012{ 013public static void main (String args[]) 014 { 015 java.io.FileWriter outputFile; 016 java.io.BufferedWriter writer; 017 java.io.PrintWriter output; 018 String fileName = "test.txt"; 019 if (args.length > 0) 020 { fileName = args[0]; } 021 String currentTime = (new java.util.Date()).toString(); 022 try 023 { 024 outputFile = new java.io.FileWriter(fileName); 025 writer = new java.io.BufferedWriter(outputFile); 026 output = new java.io.PrintWriter(writer); 027 output.println("Writing to file ".concat(fileName)); 028 output.println("The time is ".concat(currentTime)); 029 output.println("This is a test"); 030 output.close(); 031 } 032 catch (java.io.IOException e) 033 { 034 System.out.println("IOException encountered"); 035 } 036 } 037}