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.*;
class CreateFile
{
public static void main ( String[] args )
{
// Get filename and create the file
FileWriter writer = null;
BufferedReader user = new BufferedReader(
new InputStreamReader( System.in ) );
String fileName = "";
System.out.print("Enter Filename-->"); System.out.flush();
try
{
fileName = user.readLine().trim();
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 );
}
}
}