go to previous page   go to home page   go to next page

Answer:

Yes.


PrintWriter

import java.io.*;

class WriteTextFile 
{

  public static void main ( String[] args ) throws IOException
  {
    PrintWriter  output = new PrintWriter( "myOutput.txt" );

    output.println( "The world is so full"  );  
    output.println( "Of a number of things,"  );  
    output.println( "I'm sure we should all" );  
    output.println( "Be as happy as kings."  );  

    output.close();
  }
}

PrintWriter is used to send characters to a text file. Above is a program that creates the file myOutput.txt and writes several lines of characters to that file. The constructor creates an object for the file and also creates a disk file:

PrintWriter  output = new PrintWriter( "myOutput.txt"  );

If the file already exists its contents will be destroyed unless the user does not have permission to alter the file. Operating systems implement file permissions where files can be made read only. An IOException is thrown if there is a problem creating the file.

The program sends several lines of characters to the output stream for the file. Each line ends with the control characters that separate lines:

output.println( "The world is so full"  );  
output.println( "Of a number of things,"  );  
output.println( "I'm sure we should all" );  
output.println( "Be as happy as kings."  );  

Finally, the stream is closed. This means that the disk file is completed and now is available for other programs to use.

output.close();

The file would be closed when the program ended without using the close() method. But sometimes you want to close a file before your program ends.


QUESTION 12:

Could the TYPE command be used in the Windows command window (the DOS prompt window) to see the contents of this file?