De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Hoofdstuk 13.6 File-I/O. Files n40 verschillende klassen in library java.io Exceptions niet meegeteld... nMethodes voor het plegen van Input en Output.

Verwante presentaties


Presentatie over: "Hoofdstuk 13.6 File-I/O. Files n40 verschillende klassen in library java.io Exceptions niet meegeteld... nMethodes voor het plegen van Input en Output."— Transcript van de presentatie:

1 Hoofdstuk 13.6 File-I/O

2 Files n40 verschillende klassen in library java.io Exceptions niet meegeteld... nMethodes voor het plegen van Input en Output nHiërarchisch geordend d.m.v. subklassen

3 Klassen voor bewerking van files behandel de file als geheel bewerk de inhoud van de file benader de file nu eens hier, dan eens daar benader de file in volgorde Object File Random AccessFile Stream

4 Soorten Streams iedere byte apart beschikbaar multi-byte conversies lezen schrijven lezen schrijven Random AccessFile Stream Input Stream Output Stream Reader Writer Byte Stream Character Stream

5 Lezen uit een InputStream nint n; n = inpStr.read( ); if (n==-1)tf.setText(“niets gelezen”); elsetf.setText(“gelezen: ” + n ); nbyte [ ] a; n = inpStr.read(a); for (i=0; i<n; i++) verwerk( a[i] );

6 Creëren van een InputStream File InputStream Input Stream FileInputStream fis; fis = new FileInputStream(“hallo.txt”); n = fis.read(); subklasse van InputStream kent ook read

7 Creëren van een InputStream File InputStream ByteArray InputStream Input Stream ByteArrayInputStream bis; bis = new ByteArrayInputStream( ba ); n = bis.read(); byte [ ] ba; ba = new byte[100]; for (t...) ba[t] =...;

8 Creëren van een InputStream DataInputStream dis; dis = new DataInputStream( ); d = dis.readDouble(); eerder gemaakte InputStream fis File InputStream ByteArray InputStream File / String byte [ ] InputStream Buffered InputStream Data InputStream Direct InputStream Input Stream Filter InputStream

9 Creëren van een OutputStream File OutputStream ByteArray OutputStream File / String OutputStream Buffered OutputStream Data OutputStream Direct OutputStream Output Stream Filter OutputStream

10 Creëren van een Reader InputStream Reader String Reader InputStream String Reader Buffered Reader Filter Reader Direct Reader Indirect Reader

11 Totaal overzicht File-I/O convenience klassen Buffered InputStream Data InputStream File OutputStream ByteArray OutputStream Buffered OutputStream Data OutputStream PrintStream InputStream Reader String Reader Buffered Reader OutputStream Writer String Writer Buffered Writer Print Writer File InputStream ByteArray InputStream Filter InputStream Filter OutputStream Random AccessFile File / String byte [ ] InputStream File / String OutputStream InputStream String Reader OutputStream Writer Writer / OutputStream File Reader PushBack Reader File / String Reader File / String File Writer LineNumber Reader Filter Reader Filter Writer Direct InputStream Direct OutputStream Direct Reader Direct Writer Indirect Writer Input Stream Byte Stream Character Stream Indirect Reader Output Stream Reader Writer Stream

12 Soorten Files: keuzes nHele file of inhoud lezen? nStream of RandomAccess ? nCharacterStream of ByteStream ? nInput of Output ? n“echte” File of indirect?

13 Gebruik van I/O-klassen FileReader fr; fr = new FileReader ( “aap.txt” ); char c; c = fr. read (); BufferedReader br; br = new BufferedReader ( fr ); String s; s = br. readLine ( ); filenaam geef de FileReader in “beheer” aan de BufferedReader

14 Dialogen window met eigen menu hoort bij een ander window specifieke dialoog voor file-naam Component TextArea TextField Panel Window Applet Frame DialogFileDialog Button Canvas Label Scrollbar Text Component Container

15 class Hallo... { Hallo ( ) { } public void actionPerformed(...) { } Een dialoog voeren FileDialog fd; fd = new FileDialog( this, “Open File...”, FileDialog.LOAD ); fd. show ( ); s = fd. getFile ( ); “parent” window of SAVE retourneert filenaam of null

16 Editor-programma: actie public void actionPerformed (ActionEvent e) { e.getSource()(MenuItem) ( )( ). getLabel() String keus; keus = ; if (keus. equals (“Quit”)) System.exit(0); else if (keus. equals (“Open”)) this. open ( ); else if (keus. equals (“Save”)) this. save ( ); else if...

17 Editor-programma: open private void open ( ) { openDial. show ( ); String naam; naam = openDial. getFile ( ); if (naam != null) this. lees (naam);

18 Editor-programma: lees private void lees (String naam) { fr = new FileReader (naam); br = new BufferedReader ( fr ); FileReader fr; BufferedReader br; String regel; regel = br. readLine ( ); tekstveld. append ( regel + “\n” ); while ( regel != null ) { regel = br. readLine ( ); } try { } catch (...) {...}

19 Editor-programma: main public static void main (String [ ] params) { Editor e; e = new Editor ( ); e. setVisible (true); if (params.length > 0) e. lees ( params[0] ); } lees alvast een file in zoals gespecificeerd op de commandoregel

20 Totaal overzicht File-I/O Buffered InputStream Data InputStream File OutputStream ByteArray OutputStream Buffered OutputStream Data OutputStream PrintStream InputStream Reader String Reader Buffered Reader OutputStream Writer String Writer Buffered Writer Print Writer File InputStream ByteArray InputStream Filter InputStream Filter OutputStream Random AccessFile File / String byte [ ] InputStream File / String OutputStream InputStream String Reader OutputStream Writer Writer / OutputStream File Reader PushBack Reader File / String Reader File / String File Writer LineNumber Reader Filter Reader Filter Writer Direct InputStream Direct OutputStream Direct Reader Direct Writer Indirect Writer Input Stream Byte Stream Character Stream Indirect Reader Output Stream Reader Writer Stream


Download ppt "Hoofdstuk 13.6 File-I/O. Files n40 verschillende klassen in library java.io Exceptions niet meegeteld... nMethodes voor het plegen van Input en Output."

Verwante presentaties


Ads door Google