02CBI - M. Morisio. I/O in Java. Input Output. Classi di Primo Livello - java.io. Stream. Pag. 1

Documenti analoghi
INPUT OUTPUT Programmazione in rete e laboratorio. Le operazioni di I/O avvengono attraverso stream (sequenze di byte)

Lettura e scrittura - Stream

Input/Output in Java

Il linguaggio Java I file sequenziali

Laboratorio Reti di Calcolatori Laurea Triennale in Comunicazione Digitale. Anno Accademico 2012/2013

Introduzione. Java. Streams. Streams

Fondamenti di Informatica C Esercitazioni di Laboratorio / 4 Outline

RETI DI CALCOLATORI Linguaggio Java: Il Package di Input Output

Il linguaggio Java. Gli stream

domenica 9 giugno 13 Serializzazione

Corso sul linguaggio Java

Programmazione 2. Input Output su Stream e File

Laboratorio di reti I: Java IO

Lezione 18 Le classi per l'input/output nel pacchetto java.io

Il sistema I/O di Java

789:;<:' .&+/"0&12%34%5&66+,("%3787%% %"+&%88$77%9%8:$:7%% ;<'&12%8%=,+>"%3787 % % % %"+&%88$77%9%8:$:7%

Uno stream o flusso di dati o canale è un percorso di comunicazione tra la sorgente di una certa informazione e la sua destinazione.

Gestione dell I/O su File

20 - Input/Output su File

Esercitazioni Ingegneria del So2ware 3 - Programmazione Java Excep<ons, I/O

Lezione 16 programmazione in Java

Programmazione in Java e gestione della grafica. Lezione 24

Corso sul linguaggio Java

Variabili e Metodi di classe Interfacce e Package Gestione di File in Java

Flussi, lettori e scrittori

File e Stream In Java. Prof. Francesco Accarino IIS Sesto San Giovanni Via Leopardi 132

Operazioni di input/output. Prof. Francesco Accarino IIS Altiero Spinelli Via Leopardi 132 Sesto San Giovanni

Gestione di file in Java

Creazione, eliminazione, lettura e scrittura di file di testo

La Serializzazione in Java. L24_Serialization 1

File, flussi e pacchetto java.io

Informatica I. Ingegneria Medica. Prof. Diego Salamon

Unità 2 I file binari

Input e Output in Java

Un flusso (stream) è una astrazione che produce e/o consuma informazioni.

ProgrammazioneJava. Davide Di Ruscio Dipartimento di Informatica Università degli Studi dell Aquila.

JAVA INTRODUZIONE C++ JDK e JRE API IDE (BLUEJ) JAVA Introduzione. Versioni e IDE DIAPOSITIVA 2 ALESSANDRO URSOMANDO

C. Horstmann Fondamenti di programmazione e Java 2 3^ edizione Apogeo

Libreria standard JAVA

Creazione, eliminazione, lettura e scrittura di file di testo

Tecnologia e Applicazioni Internet 2010/11

Prof. D. Malerba Dr. A. Appice - Dr. M.Ceci. Il sistema Input/Output di Java

Operazioni di scrittura e lettura con periferici in linguaggio Java

7. Array e Collection 8. Progetto di classi 9. Ereditarietà 10. Eccezioni 11. Stream 5. Definire metodi 6. Strutture di controllo.

La gestione dell input/output da tastiera La gestione dell input/output da file La gestione delle eccezioni

Sviluppo Applicazioni Mobile Lezione 13. Dr. Paolo Casoto, Ph.D

Programmazione ad Oggetti

Cifratura simmetrica

Unità A1 I file testo

Restrizioni per la sicurezza

L input da tastiera in Java. Dott. Ing. M. Banci, PhD

Eccezioni Precisazioni e approfondimenti

StringTokenizer: esempio. Lettura di matrici da file. Come procedere? Lettura matrice: esempio - I. La classe StringTokenizer

Fondamenti di Informatica 1. Prof. B.Buttarazzi A.A. 2010/2011

Esercitazione OBIETTIVI DELL ESERCITAZIONE

Sockets in Java. Lorenzo Gallucci

Laboratorio di Programmazione II Corso di Laurea in Bioinformatica Dipartimento di Informatica - Università di Verona

LPR A 2005/2006 Lezione 4

Costruire le Classi, e la Gestione dei File. Lezione 8

La gestione di file e flussi in Java: note introduttive

RETI DI CALCOLATORI Linguaggio Java: Eccezioni

ProgettAzione tecnologie in movimento - V anno Unità 4 - Realizzare applicazioni per la comunicazione in rete

Programmazione ad Oggetti. Java Parte II

Corso di Reti di Calcolatori L-A

In un file di testo galera.txt sono memorizzati i dati relativi ad alcune condanne penali, ognuna con i dati:

Laboratorio di Reti Lezione 3. JAVA IO Abstraction Streams. 8/3/2016 Laura Ricci

Riferimenti ad oggetti: Fondamenti di Informatica L-B Esercitazione n 4 Java: I/O, Costruttori e altro. Riferimenti ad oggetti: (Esempio)

C# I FILES. Ad esempio, se volessimo memorizzare le seguenti informazioni relative ad una persona: Nome Telefono Indirizzo

10. File e loro attributi

10.4. La classe File e gli attributi di directory e file

Linguaggi Corso M-Z - Laurea in Ingegneria Informatica A.A I/O, thread, socket in Java

Riassunto. I mattoni di base di un programma Java. Oggi. Un programma complicato. Oggi. Perché è complicato? Stefano Mizzaro 1.

Organizzazione della lezione. Lezione 10 Interprocess Communication in Java. Gli obiettivi del middleware. Middleware

Corso di Reti di Calcolatori. java.net.url

Capitolo 13 Java IO. Software per TLC - AA 2008/2009 1

Prima lezione di laboratorio. I mattoni di base di un programma Java. Comandi Unix. Riassunto. Oggi. Un programma complicato.

Java e Serializzazione dalla A all'xml di Leonardo Puleggi

(VHUFLWD]LRQLGLEDVHVXOOH6RFNHWLQ-DYD 6RFNHWGLWLSRVWUHDP

Architettura Client-Server

Laboratorio di programmazione di rete - Corso B - Anno accademico Progetto conclusivo. Laboratorio di Programmazione di Rete

Esercitazione 6. Tutor: Ing. Diego Rughetti. Anno Accademico 2007/2008

18 - Vettori. Programmazione e analisi di dati Modulo A: Programmazione in Java. Paolo Milazzo

Il pattern decorator. L21_DecoratorPattern 1

10. File e loro attributi

Tecnologie Web L-A. Java e HTTP. Dario Bottazzi Tel , dario.bottazzi@unibo.it, SkypeID: dariobottazzi. Java e TCP/IP in a Nutshell

Iterazioni. L istruzione while. while (condition) istruzione

Gestione delle Eccezioni

Chat. Si ha un server in ascolto sulla porta Quando un client richiede la connessione, il server risponde con: Connessione accettata.

Modificatori. Controllo di visibilità

IL LINGUAGGIO JAVA Input, Tipi Elementari e Istruzione Condizionale

Il numero di classi è assurdo, e pensare che originariamente il sistema I/O java era pensato per limitare l esplosione delle classi!

Prima lezione di laboratorio. I mattoni di base di un programma Java. Riassunto. Comandi Unix. Oggi. Blog & Group.

Polimorfismo parametrico vs polimorfismo per inclusione

Java, Oggetti e Strutture Dati di G. Callegarin - Edizioni CEDAM

Linguaggio Java. Andrea Domenici Scuola Superiore S.Anna

5 - I package di base in Java

Lavorare con i File e gestire le Eccezioni

Serializzazione Java. Serializzazione. Calendario esercitazioni e laboratori. Applicazioni della Serializzazione

Capitolo 6 - Array. Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

STRINGHE IN JAVA In Java, le stringhe non sono pezzi di memo-ria con dentro dei caratteri, come in C: sono oggetti appartenenti alla classe

Transcript:

Input Output I/O in Java Stream Buffer File StringTokenizer, StreamTokenizer Serializzazione Stream Stream Tutte le operazioni di I/O si riferiscono all astrazione dello STREAM (flusso di byte) Uno stream può essere: un file su disco gli standard input, output, error una connessione di rete un flusso di dati associato a qualunque periferica Le operazioni di I/O funzionano nello stesso modo su tutti i tipi di stream Stream Classi di Primo Livello - java.io Reader Writer stream di char (caratteri Unicode 16 bit) qualsiasi carattere InputStream OutputStream stream di byte (8 bit) dati binari, suoni, immagini package java.io tutte le exception relative sono sottoclassi di IOException InputStream +read():int... OutputStream +write(int)... Classi per I/O di sequenze di byte Object Reader +read():int... Writer +write(int)... Classi per I/O di sequenze di caratteri Unicode StreamTokenizer File Pag. 1

java.io - Reader (chars) Reader (abstract) void close() Close the stream. void mark(int readaheadlimit) Mark the present position in the stream. boolean marksupported() Tell whether this stream supports the mark() operation. int read() Read a single character -1 when end of stream will block until char is available, I/O error, end of stream int read(char[] cbuf) Read characters into an array. abstract int read(char[] cbuf, int off, int len) Read characters into a portion of an array. java.io - Writer (chars) boolean ready() Tell whether this stream is ready to be read. void reset() Reset the stream. long skip(long n) Skip characters. Writer (abstract) close() close the stream, flushing it first. abstract void flush() Flush the stream. Void write(char[] cbuf) Write an array of characters. Abstract void write(char[] cbuf, int off, int len) Write a portion of an array of characters. Void write(int c) Write a single character. Void write(string str) Write a string. Void write(string str, int off, int len) Write a portion of a string. java.io - InputStream (bytes) Pag. 2

InputStream int available() Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. Void close() Closes this input stream and releases any system resources associated with the stream. Void mark(int readlimit) Marks the current position in this input stream. Boolean marksupported() Tests if this input stream supports the mark and reset methods. Abstract int read() Reads the next byte of data from the input stream. Int read(byte[] b) Reads some number of bytes from the input stream and stores them into the buffer array b. int read(byte[] b, int off, int len) Reads up to len bytes of data from the input stream into an array of bytes. Void reset() Repositions this stream to the position at the time the mark method was last called on this input stream. long skip(long n) Skips over and discards n bytes of data from this input stream. java.io - OutputStream (bytes) OutputStream void close() Closes this output stream and releases any system resources associated with this stream. void flush() Flushes this output stream and forces any buffered output bytes to be written out. Void write(byte[] b) Writes b.length bytes from the specified byte array to this output stream. Void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this output stream. abstract void write(int b) Writes the specified byte to this output stream. in out System definisce lo stream in e out class System { static InputStream in; static PrintStream out; // PrintStream definita // dopo Specializzazioni di stream Memory pipe file buffered printed interpreted Pag. 3

RW in memory CharArrayReader CharArrayWriter ByteArrayInputStream ByteArrayOutputStream RW in memory StringReader StringWriter RW di char da/a String StringBufferInputString legge bytes da StringBuffer RW di char o byte da/a array in memoria RW of Pipe PipedReader PipedWriter RW di char da pipe PipedInputStream PipedOutputStream RW di byte da pipe pipe servono per la comunicazione tra thread RW of File FileReader FileWriter RW di char da file FileInputStream FileOutputStream RW di byte da file File gestione di filename e pathname File File abstract pathname directory, file, fileseparator absolute, relative converte abstract pathname <--> string metodi: create() delete() exists(), mkdir() getname() getabsolutepath(), getpath(), getparent(), isfile(), isdirectory() ishidden(), length() listfiles(), renameto() Pag. 4

Esempio copiare file su un altro import java.io.*; public class Copy { public static void main(string[] args) throws IOException { File inputfile = new File("farrago.txt"); File outputfile = new File("outagain.txt"); FileReader in = new FileReader(inputFile); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read())!= -1) out.write(c); in.close(); out.close(); buffered BufferedInputStream BufferedOutputStream BufferedReader readline() BufferedWriter bufferizzano l IO BufferedInputStream(InputStream i) BufferedInputStream(InputStream i, int size) Consumer.. read() Buffer (eventuale) BufferedInputStream Stream (FileInputStream, PipedInputStream, StringReader.. Source (File, Pipe, String,..) File in = new File( in.txt ); BufferedInputStream b = new BufferedInputStream (new FileInputStream( in));.. while (b.read!= -1 ) b.read(); printed interpreted PrintStream(OutputStream o) print() println() per tipi primitivi e String non throw IOException, ma setta bit, da vedere con checkerror() System definisce out e err class System { static PrintStream out, err; Trasformano tipi primitivi Java in rappresentazione standard (UTF-8) su file DataInputStream(InputStream i) readbyte(), readchar(), readdouble(), readfloat(), readint(), readlong(), readshort(),.. readline() deprecated (usare BufferedReader) DataOutputStream(OutputStream o) analoghi write() Pag. 5

import java.io.*; public class DataIODemo { public static void main(string[] args) throws IOException { // write the data out DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt")); double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 ; int[] units = { 12, 8, 13, 29, 50 ; String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain"; for (int i = 0; i < prices.length; i ++) { out.writedouble(prices[i]); out.writechar('\t'); out.writeint(units[i]); out.writechar('\t'); out.writechars(descs[i]); out.writechar('\n'); out.close(); // read it in again DataInputStream in = new DataInputStream(new FileInputStream("invoice1.txt")); double price; int unit; StringBuffer desc; double total = 0.0; while (true) { price = in.readdouble(); in.readchar(); // throws out the tab unit = in.readint(); in.readchar(); // throws out the tab char chr; desc = new StringBuffer(20); char linesep = System.getProperty("line.separator").charAt(0); while ((chr = in.readchar())!= linesep) desc.append(chr); System.out.println("You've ordered " +unit + " units of " + desc + " at $" + price); total = total + unit * price; catch (EOFException e) { System.out.println("For a TOTAL of: $" + total); in.close(); Conversione byte - char InputStreamReader byte --> char OutputStreamWriter char --> byte Tokenizers StringTokenizer lavora su String set di delimiter (default: blank,,, \t, \n, \r, \f ) spezza la String in token (separati dai delimiter), rende token hasmoretokens(), nexttoken() non distingue identificatori, numeri, commenti, quoted strings Tokenizers Esempi StreamTokenizer lavora su Stream (Reader) piu sofisticato, riconosce identifiers, commenti, quoted string, numeri usa tabella di simboli e flag nexttoken(), TT_EOF se fine Pag. 6

Leggere da tastiera int c; c = System.in.read(); // System.in e InputStream, legge byte, non char catch (IOException e) { // per leggere char occorre wrappare System.in con un reader che decodifichi InputStreamReader is = new InputStreamReader (System.in); c = is.read(); catch (IOException e) { // con il ciclo InputStreamReader is = new InputStreamReader (System.in); while ( (c = is.read())!= -1) // c disponibile catch (IOException e) { Leggere da tastiera // read() possono essere inefficienti, meglio bufferizzare BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); while ( (c = br.read())!= -1) // c disponibile catch (IOException e) { // oppure procedere a righe BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); String s; while ( s = br.readline() )!= null) // s disponibile catch (IOException e) { Leggere da file Interpretare una riga //analogamente BufferedReader br = new BufferedReader (new FileReader ( MioFile.txt )); while ( (c = br.read())!= -1) // c disponibile catch (IOException e) { // oppure procedere a righe BufferedReader br = new BufferedReader (new FileReader ( MioFile.txt )); String s; while ( s = br.readline() )!= null) // s disponibile catch (IOException e) { // riga letta disponibile in s // il numero di token e noto, ad es nome, eta, reddito String s; StringTokenizer st = new StringTokenizer(s); String nome = st.nexttoken(); int eta = Integer.parseInt(st.nextToken()); double reddito = Double.parseDouble(st.nextToken()); Interpretare una riga Scrivere file // riga letta disponibile in s // il numero di token non e noto String s; StringTokenizer st = new StringTokenizer(s); while (st.hasmoretokens()){ // trattare st.nexttoken() // FileWriter scrive caratteri su file // BufferedWriter bufferizza per migliorare efficenza, solo metodo write // PrintWriter offre println PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( File.txt ))); out.println( ); catch(ioexception e){ Pag. 7

Esempio Scrivere un programma in grado di scrivere un array di interi su un file rileggere gli interi nell array class IntArray { private int interi[]; public IntArray(){ interi = new int[30]; for (int i=0; i<interi.length; i++){ interi[i] = i+1; public void scarica(printwriter out){ out.println("array di interi "); for (int i=0; i<interi.length; i++){ out.print(interi[i] + " "); // tutti su una riga ; out.flush(); out.close(); public void carica(bufferedreader br){ String s=null; s = br.readline(); // skip prima riga s = br.readline(); catch (IOException e) { StringTokenizer st = new StringTokenizer(s); int i = 0; while (st.hasmoretokens()){ interi[i++] = Integer.parseInt(st.nextToken()); public static void main(string[] args) { IntArray a = new IntArray(); PrintWriter out = new PrintWriter (new BufferedWriter( new FileWriter("prova.txt"))); a.scarica(out); BufferedReader br = new BufferedReader (new FileReader ("prova.txt" )); a.carica(br); catch (IOException e){ // varianti: carica(string s) e scarica(string s) ricevono s = nome file Serializzazione Per leggere/scrivere un oggetto occorre: leggere/scrivere gli attributi (ed eventualmente il tipo) dell oggetto separare correttamente i vari elementi in lettura, creare l oggetto e impostarne tutti gli attributi queste operazioni (serializzazione) sono automatizzate da ObjectInputStream ObjectOutputStream Uso della Serializzazione I metodi per leggere/scrivere oggetti sono: void writeobject(object) Object readobject() Possono essere serializzati solo gli oggetti che implementano l interfaccia Serializable questa interfaccia non contiene metodi serve soltanto per evitare che possano essere serializzati oggetti senza il consenso di chi ha scritto la classe Pag. 8

Ripristino del Tipo In fase di lettura, poiché viene creato un Object, occorre fare un downcasting per ripristinare il tipo originale dell oggetto Problema: come sapere il tipo originale? In realtà, non sempre è necessario il downcasting al tipo esatto: serve solo se avrò bisogno di inviare messaggi specifici Per evitare il problema di identificare il tipo degli oggetti, il downcasting può essere fatto ad un progenitore comune Esempio array di interi class IntArray implements Serializable { private int interi[]; public IntArray(){ public void scarica(printwriter out){ // no change public void carica(bufferedreader br){ // no change public static void main(string[] args) { // salvare ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("provaserial.txt")); os.writeobject(a); catch (FileNotFoundException e) { catch (IOException e) { public static void main(string[] args) { // caricare ObjectInputStream is = new ObjectInputStream(new FileInputStream("provaserial.txt")); a = (IntArray) is.readobject(); catch (FileNotFoundException e) { catch (IOException e) { catch (ClassNotFoundException e) { Salvataggio di Oggetti con Riferimenti Un ObjectOutputStream salva automaticamente anche tutti gli oggetti cui puntano gli attributi gli oggetti serializzati vengono numerati sullo stream i reference vengono salvati come numeri d ordine nello stream Se salvo due oggetti che puntano ad un terzo, questo viene salvato una sola volta prima di salvare un oggetto, l ObjectOutputStream controlla che non sia già stato salvato in caso contrario salva solo il riferimento (come numero) Esempio Gestione studenti serializzare Pag. 9

public class Studente implements Serializable { // no change public class StudentSet implements Serializable { // no change public static void main(string[] args) { // salvataggio su file Studente s1, s2, s3; s1= new Studente("Mario", "Rossi", 1234); s2= new Studente("Gianni", "Bianchi", 1243); StudentSet ss = new StudentSet(); ss.add(s1); ss.add(s2); ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("studenti.txt")); os.writeobject(ss); os.close(); catch (FileNotFoundException e) { catch (IOException e) public static void main(string[] args) { StudentSet ss =null; // caricamento da file ObjectInputStream is = new ObjectInputStream(new FileInputStream("studenti.txt")); ss = (StudentSet) is.readobject(); catch (FileNotFoundException e) { catch (IOException e) { catch (ClassNotFoundException e) { ss.print(); Pag. 10