1 / 22

Streams and Files

Streams and Files. Να μάθουμε τα βασικά για Java’s IO package Να καταλάβουμε την διαφορά ανάμεσα σε ένα text και ένα binary files Να κατανοήσουμε την ιδέα του input or output “stream” Εισαγωγή στα exceptions Πρακτική με EasyReader and EasyWriter. Στόχοι :.

fwilson
Télécharger la présentation

Streams and Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Streams and Files

  2. Να μάθουμε τα βασικά για Java’s IO package Να καταλάβουμε την διαφορά ανάμεσα σε ένα text και ένα binary files Να κατανοήσουμε την ιδέα του input or output “stream” Εισαγωγή στα exceptions Πρακτική μεEasyReader and EasyWriter Στόχοι: Δρ. Μαρία Ι. Ανδρέου

  3. ένα file είναι μια συλλογή από data in mass storage. ένα data file ΔΕΝείναι μέρος του source code του προγράμματος. Το ίδιο file μπορεί να διαβαστεί και να τροποποιηθεί από διάφορα different programs. Το πρόγραμμα πρέπει να ξέρει το format των data μέσα στο file. Files Δρ. Μαρία Ι. Ανδρέου

  4. Το 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. Files (συνέχ.) Δρ. Μαρία Ι. Ανδρέου

  5. ένα 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). Text Files Δρ. Μαρία Ι. Ανδρέου

  6. Παραδείγματα: 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.) Text Files Δρ. Μαρία Ι. Ανδρέου

  7. Ένα “binary” file περιλαμβάνει οποιαδήποτε information, οποιοδήποτε συνδυασμό από bytes. Μόνο ένας programmer / designer ξέρει πώς να το μεταφράσει. Διαφορετικά προγράμματα μπορούν να μεταφράσουν το ίδιο file διαφορετικά (e.g., one program displays an image, another extracts an encrypted message). Binary Files Δρ. Μαρία Ι. Ανδρέου

  8. Παραδείγματα: 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). Binary Files Δρ. Μαρία Ι. Ανδρέου

  9. Text as Binary: ASCII display rose.txt A rose is a rose is a rose Hex “dump” CR + LF Δρ. Μαρία Ι. Ανδρέου

  10. ένα “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. Streams Δρ. Μαρία Ι. Ανδρέου

  11. ένα file μπορεί να θεωρηθεί σαν ένα input or output stream. Στην πραγματικότητα file streams είναιbufferedγια μεγαλύτερη αποδοτικότητα: δεν είναι πρακτικό να διαβάζουμε και να γράφουμε ένα ένα χαρακτήρα από mass storage. Είναι συνηθισμένο να αντιμετωπίζουμε τα text files σαν streams. Streams (συνέχ.) Δρ. Μαρία Ι. Ανδρέου

  12. Ένα πρόγραμμα μπορεί να αρχίσει να διαβάζει ή να γράφει (reading or writing)σε ένα random-access file σε οποιοδήποτε σημείο και μπορεί να τοποθετεί και να διαβάζει οποιοδήποτε αριθμό από bytes κάθε φορά. “Random-access file” είναι μια abstraction: οποιοδήποτε file μπορεί να θεωρηθεί σαν random-access file. Μπορείτε να ανοίξετε (open) a random-access file both for reading and writing at the same time. Random-Access Files Δρ. Μαρία Ι. Ανδρέου

  13. Ένα binary file περιλαμβάνει fixed-length data records και είναι κατάλληλο για random-access treatment. ένα random-access file μπορεί να συνοδεύεται από ένα “index” (either in the same or a different file), που μας λέει the address of each record. Tape : CD == Stream : Random-access Random-Access Files (συνέχ.) Δρ. Μαρία Ι. Ανδρέου

  14. File Types: Summary Text File Binary Stream Random-Access common use possible, but not as common Δρ. Μαρία Ι. Ανδρέου

  15. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter DataInputStream DataOutputStream File FileDescriptor FileInputStream FileOutputStream FilePermission FileReader FileWriter FilterInputStream FilterOutputStream FilterReader FilterWriter 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? Δρ. Μαρία Ι. Ανδρέου

  16. 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. Δρ. Μαρία Ι. Ανδρέου

  17. java.io (cont’d) • 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. Δρ. Μαρία Ι. Ανδρέου

  18. 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))); Δρ. Μαρία Ι. Ανδρέου

  19. 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. Δρ. Μαρία Ι. Ανδρέου

  20. 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.” Δρ. Μαρία Ι. Ανδρέου

  21. 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 Δρ. Μαρία Ι. Ανδρέου

  22. 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 Δρ. Μαρία Ι. Ανδρέου

More Related