Savitch Java Ch' 9 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Savitch Java Ch' 9

Description:

This method calls flush before closing the file. flush. Flushes the output stream. ... outputStream.close(); System.out.println('... written to out.txt. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 14
Provided by: LewRa5
Category:
Tags: close | java | savitch

less

Transcript and Presenter's Notes

Title: Savitch Java Ch' 9


1
CHAPTER 3
File Output
2
FILE NAMES
  • Every input output file actually 2 names.
  • ? Real file name
  • ? used by operating system
  • ? use quotes when specifying inside program.
  • ? Name of the stream
  • ? connected to the file.
  • ? serves as a temporary name for file that
    is used within program.

3
TEXT FILE OUTPUT (PrintWriter)
  • TEXT FILE OUTPUT
  • import java.io.
  • Contains definitions for the class PrintWriter.
  • CREATING / DEFINING AN OUTPUT TEXT FILE
  • PrintWriter output_stream_name null
  • PrintWriter class creates a file

4
OPENING THE OUTPUT TEXT FILE
  • output_stream_name new PrintWriter
  • (new FileOutputStream(out_file_name))
  • Connects output stream to file.
  • Opens output text file.
  • Program always starts with an empty file.
  • If a file does not exist
  • New file is created
  • Opening a file that already exists eliminates the
    old file and creates a new, empty one.
  • Data in the original file is lost

5
Text File Output
  • PrintWriter output_stream_variable null
  • output_stream_variable new PrintWriter
  • (new FileOutputStream (file name) )
  • Alternative
  • PrintWriter output_stream_variable new
    PrintWriter (new FileOutputStream(file name)
    )

6
PrintWriter METHODS
  • println - output_stream_variable.println()
  • Any combination, connected with signs. The
    argument is output to the file connected to the
    stream.
  • Ends the line and the next output is sent to the
    next line.
  • print - output_stream_variable.print()
  • Any combination, connected with signs. The
    argument is output to the file connected to the
    stream.
  • Does not end the line and the next output will
    be sent to the same line.
  • close - output_stream_variable.close()
  • Closes streams connection to a file. This
    method calls flush before closing the file.
  • flush
  • Flushes the output stream. Forces an actual
    physical write to the file of any data that has
    been buffered and not yet physically written to
    the file. Normally, you should not need to
    invoke flush.

7
Closing a file
  • An Output file should be closed when you are done
    writing to it
  • Use the close method.
  • If a program ends normally it will close any
    files that are open

8
TextFileOutputDemoPart 1
  • public static void main(String args)
  • PrintWriter outputStream null
  • outputStream new PrintWriter(
  • new FileOutputStream("out.txt"))

Opening the file
Creating a file can cause the FileNotFound-Excepti
on if the new file cannot be made.
9
TextFileOutputDemoPart 2
  • System.out.println("Enter three lines of text")
  • String line null
  • int count
  • for (count 1 count lt 3 count)
  • line keyboard.readLine()
  • outputStream.println(count " " line)
  • outputStream.close()
  • System.out.println("... written to out.txt.")

Writing to the file
Closing the file
The println method is used with two different
streams outputStream and System.out
10
Appending to a file
  • PrintWriter Output_Stream_Name new PrintWriter
  • (new FileOutputStream(file_name,
    True_Boolean_Expression)
  • If the file does not exist, Java will create an
    empty file of that name and append the output to
    the end of this empty file.
  • May want to separate the declaration of the
    stream variable and the invocation of the
    constructor.
  • After this, you can use the methods println and
    print to write to the file, and the new text will
    be written after the old text in the file.

11
CLASS EXERCISE
  • Demonstrate processing until EOF
  • Write a program that copies all the lines from a
    file (story.txt) into another file
    (storylines.txt), one by one.

12
CLASS EXERCISE (SOLUTION)
  • import java.io.
  • public class FileEx4
  • public static void main (Stringargs)
  • String sentence null
  • String inputFileName "story.txt"
  • String outputFileName "storylines.txt"
  • // declaring input file
  • BufferedReader inFile null
  • // declaring output file
  • PrintWriter outFile null

13
  • //open input file
  • inFile new BufferedReader
  • (new FileReader(inputFileName) )
  • // open output file
  • outFile new PrintWriter
  • (new FileOutputStream(outputFileName) )
  • //priming read
  • sentence inFile.readLine() while
    (sentence ! null)
  • System.out.println("Copying " sentence
  • to the new file.")
  • outFile.println(sentence)
  • sentence inFile.readLine() //read next
  • inFile.close()
  • outFile.close()
  • //end main
Write a Comment
User Comments (0)
About PowerShow.com