Example Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes, since FileWriter inherits from OutputStreamWriter.

Example Program

This example program constructs a FileWriter stream. Doing so also creates a disk file in the current directory, named reaper.txt. Some calls to the write() method write characgters to the disk file, and then the file is closed.

import java.io.*;

class WriteTextFile
{

  public static void main ( String[] args ) throws IOException
  {
    String fileName = "reaper.txt" ;

    FileWriter writer = new FileWriter( fileName );

    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();
  }
}

The next pages explain the details. For now, save the program to a file and run it. Note: each time you run the program it will delete any existing file named reaper.txt and create a new one. You might wish to check that the 20 page report that you wrote last night is not named reaper.txt.

QUESTION 4:

How can you confirm that the program creates a new file?