65-lecture10 - Computer Science 65 Intro to CS February 26,...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science 65 Intro to CS February 26, 2008 Files (Chapter 4.10)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Announcements Exam #1 is Thursday, March 6 th Closed notes test You can bring in one side of a sheet of paper for notes Assignment #4 due today Assignment #5 out today, due Tuesday, March 4th
Background image of page 2
Homework #5 Step 1: Download gimp www.gimp.org http://www.gimp.org/macintosh/
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Files A ___________ is a collection of data treated as a unit and stored on a computer Programs are be able to read in data from a file: ___________________ write data to a file: _____________________
Background image of page 4
Output Files Processing an output file requires three steps We will use the PrintWriter class to write files To use the PrintWriter class, put the following import statement at the top of the source file: import java.io.*;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Opening a File To open a file for text output you create an instance of the PrintWriter class. PrintWriter outputFile = new PrintWriter(“studentFile.txt"); Pass the name of the file that you wish to open as an argument to the PrintWriter method. Warning: if the file already exists, it will be erased and replaced with a new file.
Background image of page 6
The PrintWriter Class The PrintWriter class allows you to write data to a file using the print and println methods, as you have been using to display data on the screen. Just as with System.out , the println method of the PrintWriter class will place a newline character after the written data. The print method writes data without writing the newline character.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
PrintWriter Class PrintWriter outputFile = new PrintWriter("Names.txt"); outputFile.println(“Gus"); outputFile.println("Kathryn"); outputFile.println("Jean"); outputFile.close(); Open the file. Write data to the file.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

65-lecture10 - Computer Science 65 Intro to CS February 26,...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online