Download Files

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Εισαγωγή στον Αντικειμενόστρεφη
Προγραμματισμό
(Object-Oriented Programming)
Streams and Files
Δρ. Μαρία Ι. Ανδρέου
Στόχοι:



Να μάθουμε τα βασικά για Java’s IO
package
Να καταλάβουμε την διαφορά ανάμεσα σε
ένα text και ένα binary files
Να κατανοήσουμε την ιδέα του input or
output “stream”

Εισαγωγή στα exceptions

Πρακτική με EasyReader and EasyWriter
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-2
Files




ένα file είναι μια συλλογή από data in
mass storage.
ένα data file ΔΕΝ είναι μέρος του source
code του προγράμματος.
Το ίδιο file μπορεί να διαβαστεί και να
τροποποιηθεί από διάφορα different
programs.
Το πρόγραμμα πρέπει να ξέρει το format
των data μέσα στο file.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-3
Files



(συνέχ.)
Το file system διατηρείται από το operating
system.
Το σύστημα παρέχει commands and/or GUI
utilities για να βλέπουμε (for viewing) τα file
directories and for copying, moving,
renaming, and deleting files.
Το σύστημα παρέχει επίσης “core” functions,
οι οποίες μπορούν να κλιθούν από
programs, for reading and writing directories
and files.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-4
Text Files



ένα computer ΧΡΗΣΙΜΟΠΟΙΕΙ distinguishes
text (“ASCII”) files και “binary” files. Αυτή η
διαφοροποίηση βασίζεται στο πως
μεταχειριζόμαστε το file.
ένα text file υποθέτετε ότι περιλαμβάνει lines
of text (e.g., in ASCII code).
Κάθε line τερματίζει με τον “newline”
character (or a combination, carriage return
plus line feed).
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-5
Text Files

Παραδείγματα:
– Any plain-text file, typically named
something.txt
– Source code of programs in any language
(e.g., Something.java)
– HTML documents
– Data files for certain programs, (e.g., fish.dat;
any file is a data file for some program.)
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-6
Binary Files



Ένα “binary” file περιλαμβάνει
οποιαδήποτε information, οποιοδήποτε
συνδυασμό από bytes.
Μόνο ένας programmer / designer ξέρει
πώς να το μεταφράσει.
Διαφορετικά προγράμματα μπορούν να
μεταφράσουν το ίδιο file διαφορετικά (e.g.,
one program displays an image, another
extracts an encrypted message).
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-7
Binary Files

Παραδείγματα:
– Compiled programs (e.g., Something.class)
– Image files (e.g., something.gif)
– Music files (e.g., something.mp3)

Any file can be treated as a binary file (even
a text file, if we forget about the special
meaning of CR-LF).
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-8
Text as Binary:
rose.txt
A rose is a rose
is a rose
Hex “dump”
ASCII
display
CR + LF
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-9
Streams



ένα “stream” είναι ένα abstraction που
προκύπτει από sequential input or output
devices.
Ένα input stream παράγει ένα stream από
characters; ένα output stream λαμβάνει ένα
stream από characters, “one at a time.”
Streams δεν εφαρμόζονται μόνο στα files,
αλλά και σε πραγματικές IO devices, Internet
streams, and so on.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-10
Streams



(συνέχ.)
ένα file μπορεί να θεωρηθεί σαν ένα input or
output stream.
Στην πραγματικότητα file streams είναι
buffered για μεγαλύτερη αποδοτικότητα: δεν
είναι πρακτικό να διαβάζουμε και να
γράφουμε ένα ένα χαρακτήρα από mass
storage.
Είναι συνηθισμένο να αντιμετωπίζουμε τα
text files σαν streams.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-11
Random-Access Files



Ένα πρόγραμμα μπορεί να αρχίσει να διαβάζει ή να
γράφει (reading or writing) σε ένα random-access
file σε οποιοδήποτε σημείο και μπορεί να τοποθετεί
και να διαβάζει οποιοδήποτε αριθμό από bytes κάθε
φορά.
“Random-access file” είναι μια abstraction:
οποιοδήποτε file μπορεί να θεωρηθεί σαν randomaccess file.
Μπορείτε να ανοίξετε (open) a random-access file
both for reading and writing at the same time.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-12
Random-Access Files (συνέχ.)



Ένα binary file περιλαμβάνει fixed-length
data records και είναι κατάλληλο για randomaccess treatment.
ένα random-access file μπορεί να
συνοδεύεται από ένα “index” (either in the
same or a different file), που μας λέει the
address of each record.
Tape : CD == Stream : Random-access
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-13
File Types: Summary
File
Text
Binary
Stream
Random-Access
common use
possible, but
not as common
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-14
java.io
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
ByteArrayInputStream
ByteArrayOutputStream
CharArrayReader
CharArrayWriter
DataInputStream
DataOutputStream
File
FileDescriptor
FileInputStream
FileOutputStream
FilePermission
FileReader
FileWriter
FilterInputStream
FilterOutputStream
FilterReader
FilterWriter
Εισαγωγή στο ΟΟΡ και Java
InputStream
InputStreamReader
LineNumberInputStream
LineNumberReader
ObjectInputStream
ObjectInputStream.GetField
ObjectOutputStream
ObjectOutputStream.PutField
ObjectStreamClass
ObjectStreamField
OutputStream
OutputStreamWriter
PipedInputStream
PipedOutputStream
PipedReader
PipedWriter
PrintStream
PrintWriter
PushbackInputStream
PushbackReader
Δρ. Μαρία Ι. Ανδρέου
RandomAccessFile
Reader
SequenceInputStream
SerializablePermission
StreamTokenizer
StringBufferInputStream
StringReader
StringWriter
Writer
How do I
read an int
from a file?
1-15
java.io



(συνέχ.)
Χρησιμοποιεί τέσσερις hierarchies of classes
rooted at Reader, Writer, InputStream,
OutputStream.
Has a special stand-alone class
RandomAccessFile.
Διαφορετικές input classes παρέχουν
methods για reading ένα single byte και byte
arrays.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-16
(cont’d)
java.io



BufferedReader and RandomAccessFile
είναι οι μόνες classes που έχουν μια method
to read a line of text, readLine.
readLine returns a String or null αν έχουμε
φτάσει στο τέλος του file.
PrintWriter and PrintOutputStream έχουν
print and println methods παρόμοια με το
System.out’s.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-17
java.io

(συνέχ.)
Uses “wrapper” classes (a.k.a “decorators”):
a “more advanced” object is constructed
around a simpler object, adding features.
import java.io.*;
...
BufferedReader inputFile = new BufferedReader (
new FileReader (inFileName));
PrintWriter outputFile = new PrintWriter (
new BufferedWriter (
new FileWriter (outFileName)));
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-18
java.io (cont’d)

“Throws” checked exceptions when
anything goes wrong (e.g., a program fails to
open a file or encounters the end of file).

try-catch statement should be used to handle
code that throws checked exceptions.

There are no convenient methods for
reading an int or a double from an ASCII
file.
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-19
java.io (cont’d)

Interested? Read the textbook, the API Docs,
more technical books; use EasyReader.java
and EasyWriter.java as examples.

Not interested? Use EasyReader and
EasyWriter until you become a “pro.”
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-20
EasyReader
inputFile = new EasyReader (fileName);
if (inputFile.bad())
... // display and error msg, quit
int n = inputFile.readInt();
if (inputFile.bad())
... // couldn’t read, reached EOF
double x = inputFile.readDouble();
char ch = inputFile.readChar ();
String word = inputFile.readWord();
String line = inputFile.readLine();
if (line == null)
... // finished reading
lines of text
Δρ. Μαρία Ι. Ανδρέου
Εισαγωγή στο ΟΟΡ και Java
1-21
EasyWriter
outputFile = new EasyWriter (fileName);
if (outputFile.bad())
... // display and error msg, quit
// Use like System.out; has these methods
// for int, double, char, and String
outputFile.print(...);
outputFile.println(...);
outputFile.close(); // close when done
Εισαγωγή στο ΟΟΡ και Java
Δρ. Μαρία Ι. Ανδρέου
1-22
Related documents