Computer Science 111 Fundamentals of Computer Programming I - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Computer Science 111 Fundamentals of Computer Programming I

Description:

The type needed is InputStreamReader ... User may make mistake in typing ... For output files, we use a stream of type FileOutputStream ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 26
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 111 Fundamentals of Computer Programming I


1
Computer Science 111Fundamentals of Computer
Programming I
  • Introduction to files

2
News Alert
3
Why files?
  • Too much data (information) for memory.
  • Memory is volatile, and we need permanent storage.

4
Java Files
  • Files used for
  • input from file to program
  • output from program to file
  • Two means of access
  • Sequential from front to back
  • Random go directly to desired data
  • Well deal with sequential

5
Java files
  • In its simplest form, Java considers data from a
    file to be an input stream of bytes.
  • The type needed is FileInputStream

? 00110101 ? 11001100 ? 10101101 ? 01110010 ?
6
(No Transcript)
7
Java files
  • If we want to read the data as individual
    characters, we need a reader on the stream.
  • The type needed is InputStreamReader
  • The reader actually gives us the ASCII code for
    the characters as ints.

8
(No Transcript)
9
File input example
  • Essentially like example in book.
  • User specifies the name of a file (text file).
  • The program displays the contents of the file in
    a text area.

10
Run of program
11
Java Documentation
12
Java Documentation
13
buttonClickedOpening the file
14
InputStreamReader read method
15
Reading the filecharacter by character
16
Oops!
17
Buffered reader
  • If we want to read the file a line at a time, we
    need a buffered reader added to the input stream
    reader.
  • The type needed is BufferedReader

? 00110101 ?
? T ?
This is a little ?
18
Reading the fileline by line
19
Adding File Dialog
  • Having the user type in the file name has several
    potential pitfalls
  • User may not remember
  • User may make mistake in typing
  • Always a problem remembering where the file is
    located paths, etc.
  • Wed like to present the user with a file dialog
    to choose the file from.
  • Well show the basics by adding a file dialog to
    our example.

20
Adding a file dialog
21
File Dialog
22
Output Files
  • For output files, we use a stream of type
    FileOutputStream
  • To output text to the file, we use a PrintWriter
    on the stream.
  • PrintWriters have methods print and println
  • We can write
  • int, double, String, other types (toString used)

23
Example output file
  • Text from text area to text file

24
I'll count to three, then draw!
25
News Alert
Write a Comment
User Comments (0)
About PowerShow.com