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

Answer:

Yes.


PrintWriter

import java.io.*;

public 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 that represents the file and also creates the actual 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 the last things written to the file might be lost. If a program opens a file, it should also close the file. If a program does not explicitly close an output file some of the data sent to it might not be written before the program terminates.


QUESTION 13:

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


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