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 nHiërarchisch geordend d.m.v. subklassen
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
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
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] );
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
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] =...;
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
Creëren van een OutputStream File OutputStream ByteArray OutputStream File / String OutputStream Buffered OutputStream Data OutputStream Direct OutputStream Output Stream Filter OutputStream
Creëren van een Reader InputStream Reader String Reader InputStream String Reader Buffered Reader Filter Reader Direct Reader Indirect Reader
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
Soorten Files: keuzes nHele file of inhoud lezen? nStream of RandomAccess ? nCharacterStream of ByteStream ? nInput of Output ? n“echte” File of indirect?
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
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
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
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...
Editor-programma: open private void open ( ) { openDial. show ( ); String naam; naam = openDial. getFile ( ); if (naam != null) this. lees (naam);
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 (...) {...}
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
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