Yes. The parameter for the constructor is a string, which could contain user data.
The next example program writes
to a disk file named by the user.
The trim()
method removes leading
and trailing spaces from the user data.
Two try{}
blocks are
used so that if
the constructor throws an exception
an appropriate error message is written.
import java.io.*; import java.util.Scanner; class CreateFile { public static void main ( String[] args ) { // Get filename and create the file FileWriter writer = null; Scanner scan = new Scanner( System.in ); String fileName = ""; System.out.print("Enter Filename-->"); try { fileName = scan.next(); writer = new FileWriter( fileName ); } catch ( IOException iox ) { System.out.println("Error in creating file"); return; } // Write the file. try { writer.write( "Behold her, single in the field,\n" ); writer.write( "Yon solitary Highland Lass!\n" ); writer.write( "Reaping and singing by herself;\n" ); writer.write( "Stop here, or gently pass!\n" ); writer.close(); } catch ( IOException iox ) { System.out.println("Problem writing " + fileName ); } } }
(Review: ) What is a buffer?