Betriebssysteme Studiengang Informatik SAT n Dipl Inf Dipl
Betriebssysteme Studiengang Informatik / SAT n Dipl. -Inf. , Dipl. -Ing. (FH) Michael Wilhelm n Hochschule Harz n FB Automatisierung und Informatik n mwilhelm@hs-harz. de n http: //www. miwilhelm. de n Raum 2. 202 n Tel. 03943 / 659 338 FB Automatisierung und Informatik: BS, Dateisysteme 1
Gliederung 1. Einführung 2. Speicherverwaltung 3. Dateisysteme 4. Unix, Linux 5. Prozesse, Thread 6. Deadlocks FB Automatisierung und Informatik: BS, Dateisysteme 2
Aufbau eines Betriebssystems Kommando-Interpreter Anwenderprogramm BS-Kernel Prozessverwaltung Speicherverwaltung Log. Geräteverwaltung Ein- / Ausgabesysteme Dateisysteme Gerätetreiber Schnittstelle zur Hardware Gerätetreiber CPU / Geräte FB Automatisierung und Informatik: BS, Dateisysteme Kernel- Benutzermodus 3
Dateien, Dateisysteme n Werden zur Verwaltung von externen Speicher eingesetzt n dienen der dauerhaften Speicherung von Nutzerdaten, Programmen, BS-Daten n Speicherung via Laufwerk, Pfad, Dateinamen n Umsetzung des logischen Namens in physikalische Parameter n Zugriffszeit einer Festplatte ca. 1000. 000 -fach langsamer als Speicher n Speichervolumen ca. 80000 -fach größer (Tera-Byte) FB Automatisierung und Informatik: BS, Dateisysteme 4
Dateiverwaltung Der Hauptspeicher ist schnell und eignet sich hervorragend zur Verwaltung von Daten / Programmen! Probleme: Hauptspeicher ist begrenzt Keinen Zugriff auf eigene Daten von anderen Prozessen Bei Terminierung des Prozesses Datenverlust! FB Automatisierung und Informatik: BS, Dateisysteme 5
Lösung: Die Informationen werden auf einen externen Datenträger n Festplatte n CD-Laufwerk n DVD-Laufwerk n Blu-Ray-Laufwerk n USB-Laufwerk n Bandlaufwerk n Netzwerk n Cloud-Speicherung gespeichert. FB Automatisierung und Informatik: BS, Dateisysteme 6
Aufgaben eines Dateisystems n Erzeugen einer Datei, Verzeichnisses n Löschen einer Datei n Registrierung von Dateien (Windows, chmod Unix) n Lesen von Dateien n Schreiben in Dateien n Schutz der Datei (Rechteverwaltung, UGW=777) n Verwaltung der Metainformationen (Attribute) FB Automatisierung und Informatik: BS, Dateisysteme 7
Aufgaben eines Dateisystems (2) n Aufbau einer Grundstruktur auf dem Datenträger ÕPartitionierung ÕFormatierung ÕAufbau einer logischen Reihenfolge (1 bis n) ÕUnabhängigkeit ÕZylinder, n von der phys. Struktur Sektor, Spur sind nur dem Gerät bekannt Aufbauend auf der Blocknumerierung werden Lese- und Schreiboperationen zur Verfügung gestellt n Grundstruktur: FAT 12, FAT 16, FAT 32, NTFS, Ext 2, Ext 3, JFS FB Automatisierung und Informatik: BS, Dateisysteme 8
Aufgaben eines Dateisystems (3) Aufbau eines Superblocks, Block 0 n Wird beim adressieren eingelesen n Wird beim Booten aufgerufen n Anzahl der Partitionen (Größe, Start etc. ) n Bootprogramm n Name des Datenträger n Zeitangaben n Belegungsinformationen ÕListe aller freien Blöcke (ev. Bitinformationen) ÕListe aller belegten Blöcke (ev. Bitinformationen) ÕDefekte Blöcke FB Automatisierung und Informatik: BS, Dateisysteme 9
Eigenschaften von Dateien • Speichern beliebige Anzahl von Bytes • Die Bytes haben manchmal eine logische Bedeutung • gruppieren von Dateien mittels Verzeichnissen (directories) • Wurzelverzeichnis: speichert zusätzliche Informationen des Laufwerkes • Pfad: alle beteiligten Verzeichnisse und der Dateiname • Geräte erscheinen als Spezialdateien ohne eigene Verzeichnisstruktur (Unix) • Ein Prozess wird mit einem Arbeitsverzeichnis verknüpft • Berechtigungen werden durch uid, guid und Schutzcode nachprüfbar (Unix) 9 Bit Schutzcode, je drei Bit für Besitzer, Gruppe und Welt: rwx FB Automatisierung und Informatik: BS, Dateisysteme 10
Aufbau einer Festplatte FB Automatisierung und Informatik: BS, Dateisysteme 11
Aufbau einer Festplatte FB Automatisierung und Informatik: BS, Dateisysteme 12
Festplatte n Einteilung in - Sektoren (512 Byte) Spuren - Kopf - Zylinder - n Betriebssystem - Verwendet ein Cluster, n-faches eines Sektors Adressierung im Betriebssystem von 0 bis n-1 n Adressierung in der Festplatte mit Sektor#, Spur#, Kopf# n FB Automatisierung und Informatik: BS, Dateisysteme 13
Festplatte: Clusterdefinition des Betriebssystems Anzeige: Sektoren der Festplatte, alle 512 Byte groß A d B C FB Automatisierung und Informatik: BS, Dateisysteme 14
Festplatte: Clusterdefinition des Betriebssystems Anzeige: Sektoren der Festplatte, alle 512 Byte groß 0 7 8 n-1 x Ein BS-Cluster ist ein Vielfaches eines Sektors: 2 Bild zeigt Belegung wenn Clustergröße 2048 Bytes sind 4 Sektoren/Cluster FB Automatisierung und Informatik: BS, Dateisysteme 15
Festplatte: Clusterdefinition des Betriebssystems Anzeige: Sektoren der Festplatte, alle 512 Byte groß 7 0 8 n-1 x Ein BS-Cluster ist ein Vielfaches eines Sektors: 2 Bild zeigt Belegung wenn Clustergröße 1024 Bytes sind 2 Sektoren/Cluster FB Automatisierung und Informatik: BS, Dateisysteme 16
Laden eines Betriebssystems 1) 2) 3) 4) 5) 6) CMOS ROM, BIOS Bootsektor Auswahldialog BS Loader Kernel Oberfläche FB Automatisierung und Informatik: BS, Dateisysteme 17
Eigenschaften von Dateien: MS-DOS Windows Unix Dateinamen ignore case sensitive Verknüpfungen zu Programmen eine pro File viele Namenslänge 8. 3 255 Leerzeichen ja ja nein Filesystem FAT 16 FAT 32, NTFS ext 2, ext 3, jfs Link zu Dateien nein, jaja FB Automatisierung und Informatik: BS, Dateisysteme 18
Funktionen für Dateien n n n Create (filename) Delete (filename) handle=Open (filename, modus) Close (handle) Read (handle, buffer, count) Write (handle, buffer, count) Append (handle, buffer, count) Seek(handle, count) lock(handle) unlock(handle) set. Attrib(handle, bits) get. Attrib (handle) rename (handle, filename) FB Automatisierung und Informatik: BS, Dateisysteme 19
Programmdatei DOS: Ausführbare Dateien 1) Kopf • Magische Nummer (MZ, Mark Zbikowsky) • Textgröße • Datengröße • BSS-Größe • Symboltabellengröße • Einstiegspunkt (Viren) • Flags 2) Daten 3) Programmcode 4) Relokationsbits 5) Symboltabelle FB Automatisierung und Informatik: BS, Dateisysteme 20
Implementierung von Dateien Verfahren: kontinuierliche Allokation: Die Datei wird auf n-Blöcken hintereinander gespeichert. Vorteil: einfach zu implementieren, schnell Nachteil: keine dynamische Änderung möglich, Fragmentierung (welche) Dateigröße sollte (muss) vorher bekannt sein! Festplatte (Datei 1 bis 4): FB Automatisierung und Informatik: BS, Dateisysteme 21
Festplatte: Clusterdefinition des Betriebssystems Anzeige: Sektoren der Festplatte, alle 512 Byte groß A d B C FB Automatisierung und Informatik: BS, Dateisysteme 22
Implementierung von Dateien Verfahren: verknüpfte Liste: Block enthält Pointer und Daten Vorteil: keine Fragmentierung, Auffüllen der Block-Lücken Nachteil: sequentieller Zugriff, Blockgröße ist keine Zweierpotenz Pointer Daten FB Automatisierung und Informatik: BS, Dateisysteme 23
Implementierung von Dateien Verfahren: verknüpfte Liste: 1. Liste: Block enthält Pointer 2. Liste: Block enthält Daten Vorteil: keine Fragmentierung, Auffüllen der Block-Lücken Blockgröße ist Zweierpotenz Nachteil: Random-Zugriff Pointer Daten FB Automatisierung und Informatik: BS, Dateisysteme Pointer Daten 24
Festplatte: Clusterdefinition des Betriebssystems Anzeige: Sektoren der Festplatte, alle 4096 Byte groß A E d C F B G Datei 1: 1, 13, 25, 10, 4, 8, 28 FB Automatisierung und Informatik: BS, Dateisysteme 25
Funktionen für Verzeichnisse n Create. Dir (name) n Delete. Dir (name) n Rename. Dir (old. Name, new. Name) n Link (Name) n First. Name (Name, Bits), Traversieren einer Festplatte n Next. Name () , Traversieren einer Festplatte FB Automatisierung und Informatik: BS, Dateisysteme 26
Dateitypen Inhalt einer Datei: • ASCII-Text • beliebige Bytes • feste Records, struct • Daten in einer Baumstruktur (Winword, Excel) • Header mit unterschiedlichen Records – d. Base Datei – Grafikdateien – MP 3 -Datei Zusätzliche Daten: Dateiattribute Datum / Zeit Größe FB Automatisierung und Informatik: BS, Dateisysteme 27
Implementierung von Dateien Verfahren: verknüpfte Liste mit Indexeinsatz (FAT-12): Block: Pointer Liste: Tabelle mit den Indizes Vorteil: keine externe Fragmentierung Blöcke mit Zweier Potenz Tabelle im Hauptspeicher (FAT) Nachteil: die Tabelle muss im Hauptspeicher ständig präsent sein. Interne Fragmentierung Beispiel mit 3 Bytes Länge: 000 freier Cluster 001 -FFD Cluster belegt FFE defekt FFF letzter Cluster 4093 mögliche Cluster 80 GBytes 20 MB Clustergröße FB Automatisierung und Informatik: BS, Dateisysteme 28
Implementierung von Dateien Beispiel: Windows FAT 32 Beispiel: MS DOS FAT 16 2 Bytes: 0000 0001 -FFFD FFFE FFFF 4 Bytes: 0000 freier Cluster 01 -FFFF FFFD Cluster belegt FFFF FFFE defekt FFFF letzter Cluster 65533 mögliche Cluster 80 GBytes 1, 2 MB Clustergröße freier Cluster belegt defekt letzter Cluster 4, 29· 1009 mögliche Cluster 80 GBytes 19 Bytes Clustergröße Abhilfe: Unterteilung in Partition, Maximal vier sind möglich! FB Automatisierung und Informatik: BS, Dateisysteme 29
Implementierung von Dateien: I-nodes (UNIX) Jeder Dateieintrag enthält: I-Node Nummer: (2 Bytes, 4 Bytes, 8 Bytes) Dateinamen: 200 Zeichen 2 Bytes 14 Bytes, Dateiname I-Node Nummer Dateiname, Größe, Datum, Zeit, Plattenblöcke, Benutzerrechte FB Automatisierung und Informatik: BS, Dateisysteme 30
Suche einer Datei (I-Nodes) Beispiel: Gesucht wird die Datei: /usr/vorlesungen/bs 1) 2) 3) 4) 5) 6) 7) Bestimmen des Rootverzeichnisses /, (feste Position) Lese die I-Node Nummer vom Root, positioniere zum Verzeichnis Suche im Wurzelverzeichnisses den Eintrag „usr“ Lese den I-Node (usr) , positioniere zum Verzeichnis Suche im Verzeichnisses „usr“ den Eintrag „vorlesungen“ Lese den I-Node, , positioniere zum Verzeichnis Suche im Verzeichnisses „bs“ FB Automatisierung und Informatik: BS, Dateisysteme 31
Verweis auf Datenblöcke 1. Block Cluster 2. Block 3. Block 1. Block Cluster 8. Block 9. Block 10. Block 1 x indiz. Block 2 x indiz. Block 3 x indiz. Block 256 FB Automatisierung und Informatik: BS, Dateisysteme Cluster 32
Verweis auf Datenblöcke Cluster 1. Block 2. Block 0. Block Cluster 3. Block 255 0. Block 255 Cluster 8. Block 9. Block 10. Block 1 x indiz. Block 2 x indiz. Block 3 x indiz. Block 0. Block 255 255 FB Automatisierung und Informatik: BS, Dateisysteme Cluster 33
Einteilung nach Zugriffsformen FB Automatisierung und Informatik: BS, Dateisysteme 34
E/A Operationen in Anwendungsprogramme n Datei erzeugen n Datei schreiben n Datei lesen n Datei löschen n Datei kopieren n Verzeichnisse anlegen n Verzeichnisse löschen FB Automatisierung und Informatik: BS, Dateisysteme 35
Java: Exception public class exception 1 { public static void main(String args[]) { int j, k; j=0; k=3 / j; } // main } FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception D: HS-HarzProg 2>java exception 1 Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at exception 1. main(exception 1. java: 8) D: HS-HarzProg 2> FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception Ein perfektes Programm mit perfekten Anwendern benötigt keine Fehlerbehandlung. Alle Daten werden korrekt eingegeben, jede Datei existiert und alle Module sind richtig programmiert. Reale Welt: Beim Absturz eines Programms entstehen: • Datenverluste • Unstimmigkeiten in einer Datenbank • Neueingaben • Ärger des Anwenders Abhilfe: • Benachrichtigung an den Anwender • Sicherung der Daten • Sicheres Beenden des Programms FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception Definition: public class Exception extends Throwable The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch. Since: JDK 1. 0 Java benutzt für die Fehlerbehandlung ein „error trapping“ bzw. „exception handling“ (Delphi, C++). Diese Fehlerbehandlung ist weit flexibler als die VB Basicvariante „On error goto“. FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception Ursachen für eine Exception: • Fehlerhafte Eingabe (Numerische Eingabe, URL) • Gerätfehler (Drucker, Webseite, Diskette) • Physikalische Grenzen (mangelnder Speicher, Freier Speicherplatz) • Programmierfehler (Arrayindex, Stackfehler, Overflow, Underflow) Exception: Bei einer Exception wird • die aktuelle Prozedur sofort verlassen • es wird ein Exceptionobjekt erzeugt • es wird kein Returncode erzeugt • der Aufrufcode wird nicht weiterabgearbeitet • es beginnt die Suche nach einem „exception handler“, der für diese Exception zuständig ist FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception-Arten Standard Runtime Exceptions: Arithmetic. Exception: An exceptional arithmetic situation has arisen, such as an integer division operation with a zero divisor. Array. Store. Exception: An attempt has been made to store into an array component a value whose class is not assignment compatible with the component type of the array Class. Cast. Exception: An attempt has been made to cast a reference to an object to an inappropriate type. Illegal. Argument. Exception: A method was passed an invalid or inappropriate argument or invoked on an inappropriate object. Subclasses of this class include: Illegal. Thread. State. Exception: A thread was not in an appropriate state for a requested operation. Number. Format. Exception: An attempt was made to convert a String to a value of a numeric type, but the String did not have an appropriate format. Illegal. Monitor. State. Exception: A thread has attempted to wait on or notify other threads waiting on an object that it has not locked. Index. Out. Of. Bounds. Exception: Either an index of some sort (such as to an array, a string, or a vector) or a subrange, specified either by two index values or by an index and a length, was out of range. FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception Package java: java. io. IOException: Oberklasse alle IO-Exception A requested I/O operation could not be completed normally. Subclasses of this class include: End of file has been encountered before normal completion of an input operation. A file with the name specified by a file name string or path was not found within the file system. java. io. EOFException: java. io. File. Not. Found. Exception: java. io. Interrupted. IOException: The current thread was waiting for completion of an I/O operation, and another thread has interrupted the current thread, using the interrupt method of class Thread (§ 20. 31). java. io. UTFData. Format. Exception: A requested conversion of a string to or from Java modified UTF-8 format could not be completed (§ 22. 1. 15, § 22. 2. 14) because the string was too long or because the purported UTF-8 data was not the result of encoding a Unicode string into UTF-8. Standard Runtime Exceptions: Null. Pointer. Exception: Security. Exception: An attempt was made to use a null reference in a case where an object reference was required. A security violation was detected (§ 20. 17). FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception (Anfangsbeispiel) import java. io. *; public class exception 1 { public static void main(String argv[]) { int j, k; j=0; k=3 / j; } // main } FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception (interne Abfrage) import java. io. *; public class exception 2 { public static void main(String argv[]) { int j, k; j=0; try { // Excecption Block k=3 / j; j=k*k; save (j) // speichern in Datei } catch (Arithmetic. Exception f) { System. err. println(" Arithmetic. Exception : " + f); } Kein Absturz für den Anwender } // main } exception 2 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception (externe Abfrage) import java. io. *; public class exception 3 { public static double load. Double(String s. File. Name) { // hier könnte ein Fehler auftreten return 1. 0; // Platzhalter } public static void main(String argv[]) { double d; d = load. Double("c: \1. dat"); } // main } exception 3 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Exception (externe Abfrage) import java. io. *; public class exception 4 { public static double load. Double(String s. File. Name) throws IOException { return 1. 0; } public static void main(String argv[]) { double d; d = load. Double("c: \1. dat"); // Zwang zur Benutzung einer Exception } // main } Compilermeldung: Nicht bekannte java. IOException; muss abgefangen werden oder zum Auslösen deklariert werden Exception 3 -4 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Anwendung von Exceptions Vier Regeln: 1) Exception-Handling ersetzt nicht das Testen. Das Auslösen einer Exception kostet sehr viel Zeit. 2) Splitten von Exceptions Wenn überhaupt nur eine try Klammer mit mehreren catch-Klauseln. 3) Kein „Verstecken“ von Exceptions Eine catch-Anweisung ohne eine Meldung ist nicht sinnvoll. Tritt der Fehler auf, erhält der Anwender keine Mitteilung. 4) Also weiterreichen von Exceptions Das Verstecken einer Exception liefert „einfacheren Code“ für den „Aufrufer“. Der Aufrufer sollte aber von der Exception erfahren. Man ist kein Hellseher beim Programmieren. FB Automatisierung und Informatik: BS, Dateisysteme
Datenverarbeitung in Java Es steht eine Vielzahl von Klassen/Modulen zur Verfügung (io: 58): Eingabeklassen: gepuffert Filter für Dateinamen für Zeichen, Zeichenketten, Objekte, Token mit Pipe-Verfahren Package: io, nio 2 FB Automatisierung und Informatik: BS, Dateisysteme
Ausgabe in eine Datei gepuffert Filter für Dateinamen für Zeichen, Zeichenketten, Objekte, Token mit Pipe-Verfahren Package: io, nio 2 FB Automatisierung und Informatik: BS, Dateisysteme
Stream-Unterscheidung • ASCII: • • • Print. Stream Data. Output. Stream Line. Number. Reader Data. Input. Stream File. Reader print, println write. Bytes, write. UTF read. Line read mit Array Data. Input. Stream Data. Output. Stream read. Int … write. Int … GZIPInput. Stream GZIPOutput. Stream read Array write Array • Binär • • • ZIP • • • Serialize • • Object. Input. Stream Object. Output. Stream Object XMLInput. Factory / XMLStream. Reader XMLEncoder lesen schreiben • XML • • FB Automatisierung und Informatik: BS, Dateisysteme
Java: Ausgabe in eine ASCII-Datei import java. io. *; public class write. ASCII 01 { public static void main(String argv[]) { try { File. Output. Stream Fout = new File. Output. Stream("1. dat"); // „Druck“ausgabe in eine ASCII-Datei Print. Stream p = new Print. Stream(Fout); p. print("Hallo"); p. println("Hallo Studenten"); p. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // main } write. ASCII 02 arbeitet mit Buffered. Output. Stream FB Automatisierung und Informatik: BS, Dateisysteme
Stream-Unterscheidung • ASCII: • • • Print. Stream Data. Output. Stream Line. Number. Reader Data. Input. Stream File. Reader print, println write. Bytes, write. UTF read. Line read mit Array Data. Input. Stream Data. Output. Stream read. Int … write. Int … GZIPInput. Stream GZIPOutput. Stream read Array write Array • Binär • • • ZIP • • • Serialize • • Object. Input. Stream Object. Output. Stream Object XMLInput. Factory / XMLStream. Reader XMLEncoder lesen schreiben • XML • • FB Automatisierung und Informatik: BS, Dateisysteme
Java: Lesen einer ASCII-Datei public static void main(String argv[]) throws IOException { File. Input. Stream fin; Input. Stream. Reader iin; Line. Number. Reader din; String s. Line; try { fin = new File. Input. Stream("read. ASCII 01. java"); iin = new Input. Stream. Reader(fin); din = new Line. Number. Reader(iin); // oder Buffered. Reader br = new Buffered. Reader(ir); while ( din. ready() ) { s. Line = din. read. Line(); System. out. println(s. Line); } } catch (File. Not. Found. Exception e 1) { System. err. println("Datei war nicht vorhanden!"); } catch (IOException ee) { System. err. println("IOException: " + ee); } } FB Automatisierung und Informatik: BS, Dateisysteme read. ASCII 01. java
Java: Lesen einer ASCII-Datei private static void read(String s. Filename){ File. Reader fr; File file = new File(s. Filename); char ch; byte b; try { fr = new File. Reader( s. Filename ); //file ); String gelesen; // Der String, der am Ende ausgegeben wird // char-Array als Puffer fuer das Lesen. Die // Laenge ergibt sich aus der Groesse der Datei char[] temp = new char[(int) file. length()]; fr. read(temp); // Lesevorgang gelesen = new String(temp); // Umwandlung des char-Arrays in einen String System. out. println("Ergebnis: n"+gelesen); } catch (File. Not. Found. Exception e 1) { System. err. println("Datei nicht gefunden: " + file); } catch (IOException ee) { System. err. println("IOException: " + ee); } } FB Automatisierung und Informatik: BS, Dateisysteme read. ASCII 02. java
Java: Lesen einer ASCII-Datei private static void read(String s. Filename){ File. Input. Stream fin; Data. Input. Stream din; File file = new File(s. Filename); try { fin = new File. Input. Stream( s. Filename ); //file ); din = new Data. Input. Stream(fin); String gelesen; // Der String, der am Ende ausgegeben wird // char-Array als Puffer fuer das Lesen. Die // Laenge ergibt sich aus der Groesse der Datei byte[] temp = new byte[(int) file. length()]; din. read(temp); // Lesevorgang // Umwandlung des char-Arrays in einen String gelesen = new String(temp); System. out. println("Ergebnis: n"+gelesen); } catch (File. Not. Found. Exception e 1) { System. err. println("Datei nicht gefunden: " + file); } catch (IOException ee) { System. err. println("IOException: " + ee); } } FB Automatisierung und Informatik: BS, Dateisysteme read. ASCII 03. java
Java: Einlesen aus einer HTML-Datei public static void main(String argv[]) throws IOException { URL u = new URL("http: //www. hs-harz. de"); Input. Stream in; Data. Input. Stream din; String s; try { in = u. open. Stream(); // URL als Stream öffen din = new Data. Input. Stream(in); while ( (s=din. read. Line()) != null) { System. out. println(s); } catch (File. Not. Found. Exception e 1) { System. err. println("Datei war nicht vorhanden!"); } catch (IOException ee) { System. err. println("IOException: " + ee); } } FB Automatisierung und Informatik: BS, Dateisysteme read. ASCII 04. java
Stream-Unterscheidung • ASCII: • • • Print. Stream Data. Output. Stream Line. Number. Reader Data. Input. Stream File. Reader print, println write. Bytes, write. UTF read. Line read mit Array Data. Input. Stream Data. Output. Stream read. Int … write. Int … GZIPInput. Stream GZIPOutput. Stream read Array write Array • Binär • • • ZIP • • • Serialize • • Object. Input. Stream Object. Output. Stream Object XMLInput. Factory / XMLStream. Reader XMLEncoder lesen schreiben • XML • • FB Automatisierung und Informatik: BS, Dateisysteme
Java: Ausgabe in eine Datei (int , float, double) private void write 1() { try { File. Output. Stream fout = new File. Output. Stream("readwrite 1. bin"); // byteweise // Umbau zur Ausgabe nun mittels primitiver Datentypen Data. Output. Stream dout = new Data. Output. Stream(fout); int i=1234; dout. write. Int(i); // 4 Bytes float f = 123. 4567 f; dout. write. Float(f); double d = 123. 4567; dout. write. Double(d); System. out. println("int i: "+i); System. out. println("double d: "+d); dout. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // write 1 Readwrite_int_double_1 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Ausgabe in eine Datei (String) private void write 2() { System. out. println("nnwrite 2"); try { File. Output. Stream fout = new File. Output. Stream("readwrite 1. bin"); // byteweise // Ausgabe nun mittels primitiver Datentypen Data. Output. Stream dout = new Data. Output. Stream(fout); String s = "abcdef"; dout. write. UTF(s); System. out. println("String s: "+s); dout. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // write 2 Readwrite_int_double_1 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Einlesen aus einer Datei (int , float, double) private void read 1() { System. out. println("nnread 1"); try { File. Input. Stream fin = new File. Input. Stream("readwrite 1. bin"); // byteweise // Ausgabe nun mittels primitiver Datentypen Data. Input. Stream din = new Data. Input. Stream(fin); int i; i = din. read. Int(); // 4 Bytes float f = din. read. Float(); double d = din. read. Double(); System. out. println("int i: "+i); System. out. println("float f: "+f); System. out. println("double d: "+d); din. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // read 1 Readwrite_int_double_1 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Einlesen aus einer Datei (int , float, double) private void read 1() { System. out. println("nnread 1"); try { File. Input. Stream fin = new File. Input. Stream("readwrite 1. bin"); // byteweise // Ausgabe nun mittels primitiver Datentypen Data. Input. Stream din = new Data. Input. Stream(fin); int i; i = din. read. Int(); // 4 Bytes float f = din. read. Float(); double d = din. read. Double(); System. out. println("int i: "+i); System. out. println("float f: "+f); System. out. println("double d: "+d); din. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // read 1 Readwrite_int_double_1 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Einlesen aus einer Datei (String) private void read 2() { System. out. println("nnread 2"); try { File. Input. Stream fin = new File. Input. Stream("readwrite 1. bin"); // byteweise // Ausgabe nun mittels primitiver Datentypen Data. Input. Stream din = new Data. Input. Stream(fin); String s=""; s = din. read. UTF(); System. out. println("String s: "+s); din. close(); // es reicht din } catch (IOException e) { System. err. println("IOException: " + e); } } // read 2 Readwrite_int_double_1 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Ausgabe in eine Datei (char) private void write 1() { try { System. out. println("write 1"); File. Output. Stream fout = new File. Output. Stream("readwrite 1. bin"); // byteweise // Ausgabe nun mittels primitiver Datentypen Data. Output. Stream dout = new Data. Output. Stream(fout); int i = 12345; long ii=1234; dout. write. Int(i); // 4 Bytes dout. write. Long(ii); // 8 Bytes dout. write. Char('a'); dout. write. Char('b'); dout. write. Char('c'); // dout. write. Chars(s); // ? ? String s = "abcdef"; dout. write. UTF(s); dout. write. Double(123. 4567); dout. close(); } catch (IOException e) { System. err. println("IOException: " + e); } } // write 1 Readwrite_int_double_2 FB Automatisierung und Informatik: BS, Dateisysteme
Java: Einlesen aus einer Datei (char) … i = din. read. Int(); // 4 Bytes System. out. println("i: "+i); ii=din. read. Long(); // 8 Bytes System. out. println("long: "+ii); ch=din. read. Char(); // a System. out. println("char ch: "+ch); ch=din. read. Char(); System. out. println("char ch: "+ch); String s; //din. read. Chars(); / / gibt es nicht s = din. read. UTF(); System. out. println("String s: "+s); d = din. read. Double(); System. out. println("double d: "+d); din. close(); … Readwrite_int_double_2 FB Automatisierung und Informatik: BS, Dateisysteme
Objekte speichern und laden: Readwrite_int_double_3 FB Automatisierung und Informatik: BS, Dateisysteme
Readwrite_int_double_3 private void bn_write 1() { try { File. Output. Stream fout = new File. Output. Stream("grafik. bin"); Data. Output. Stream dout = new Data. Output. Stream(fout); Line line 1 = new Line(10, 300, 400); line 1. save(dout); dout. close(); editor. append("write 1 Line 1: "+line 1+"n"); } catch (IOException ee) { System. err. println("IOException: " + ee); } } // write 1 private void bn_read 1() { try { File. Input. Stream fin = new File. Input. Stream("grafik. bin"); Data. Input. Stream din = new Data. Input. Stream(fin); Line line 1 = new Line(0, 0, 0, 0); line 1. load(din, true); din. close(); editor. append("read 1 Line 1: "+line 1+"n"); } catch (IOException ee) { System. err. println("IOException: " + ee); } } // read 1 FB Automatisierung und Informatik: BS, Dateisysteme
class Line extends Grafik { private final int version=1; private int x 1=0; private int y 1=0; private int x 2=0; private int y 2=0; Readwrite_int_double_3 public void save(Data. Output. Stream dout ) { try { dout. write. Int(Grafik. LINE); dout. write. Int(version); dout. write. Int(x 1); dout. write. Int(y 1); dout. write. Int(x 2); dout. write. Int(y 2); } catch (IOException ee) { System. err. println("IOException: " + ee); } } FB Automatisierung und Informatik: BS, Dateisysteme
class Line extends Grafik { private final int version=1; private int x 1=0; private int y 1=0; private int x 2=0; private int y 2=0; Readwrite_int_double_3 public void load(Data. Input. Stream din, boolean mit. Typ ) { try { int typ; if (mit. Typ) typ = din. read. Int(); // besser mit unread: Pushback. Input. Stream // Readwrite_int_double_4 int version=din. read. Int(); x 1=din. read. Int(); y 1=din. read. Int(); x 2=din. read. Int(); y 2=din. read. Int(); } catch (IOException ee) { System. err. println("IOException: " + ee); } } } FB Automatisierung und Informatik: BS, Dateisysteme
Java: Methoden der Klasse Data. Output. Stream: write. Boolean write. Byte (Schreiben einer 8 -Bit Vorzeichenzahl) write. Char (Schreiben einer 16 -Bit vorzeichenlosen Zahl) write. Double (Schreiben einer Double-Zahl) write. Float (Schreiben einer Single-Zahl) write. Int (Schreiben einer 32 -Bit Vorzeichenzahl) write. Long (Schreiben einer 64 -Bit Vorzeichenzahl) write. Short (Schreiben einer 16 -Bit Vorzeichenzahl) write. UTF (Schreiben eines Strings) write. Bytes (Schreiben eines Strings) write. Chars FB Automatisierung und Informatik: BS, Dateisysteme
Java: Methoden der Klasse Data. Input. Stream: read. Boolean read. Byte (Einlesen 8 -Bit Vorzeichenzahl) read. Char (Einlesen 16 -Bit Vorzeichenzahl) read. Double (Einlesen einer Double-Zahl) read. Float (Einlesen Single-Zahl) read. Int (Einlesen 32 -Bit Vorzeichenzahl) read. Long (Einlesen 64 -Bit Vorzeichenzahl) read. Short (Einlesen 16 -Bit Vorzeichenzahl) read. UTF (Einlesen eines Strings) read. Byte (byte Array) FB Automatisierung und Informatik: BS, Dateisysteme
Int-Datentypen in Java byte from -128 to 127 short from -32768 to 32767 int from -2147483648 to 2147483647 long from -9223372036854775808 to 9223372036854775807 char from 'u 0000' to 'uffff‘, =>, from 0 to 65535 FB Automatisierung und Informatik: BS, Dateisysteme
Java Beispiele IOtest. java Laufzeit ASCII vs. Binär Read. ASCII 01. java bis Read. ASCII 04. java Write. ASCII 01. java bis Write. ASCII 02. java Schreiben ASCII readwrite. Str 1. java bis readwrite. Str 3. java Lesen und Schreiben ASCII Readwrite_int_double_1. java bis Readwrite_int_double_4. java Read_gz. java Lesen gz-Format Write_gz. java Schreiben gz-Format Read_Write_zip. java Lesen und Schreiben zip-Format (kein echtes zip-Format) Xml 1. java bis Xml 3. java Lesen einer XL-Datei FB Automatisierung und Informatik: BS, Dateisysteme
- Slides: 72